feat: implement sorting algorithms in Python
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
def selection_sort(arr):
|
||||
n = len(arr)
|
||||
for i in range(n - 1):
|
||||
# Assume the current element is the smallest
|
||||
min_index = i
|
||||
|
||||
# Checking the rest of the array to find the smallest one
|
||||
for j in range(i + 1, n):
|
||||
if arr[j] < arr[min_index]:
|
||||
min_index = j # Update the index of the smallest number
|
||||
|
||||
# If the smallest number is not the first, replace
|
||||
if min_index != i:
|
||||
arr[i], arr[min_index] = arr[min_index], arr[i]
|
||||
|
||||
# Example:
|
||||
|
||||
nums = [5, 2, 4, 6, 1, 3]
|
||||
print("Before sorting:", nums)
|
||||
|
||||
selection_sort(nums)
|
||||
print("After sorting:", nums)
|
||||
Reference in New Issue
Block a user