Selection Sort¶
Time Complexity:
- Best case: $O(n^{2})$
- Worst Case: $O(n^{2})$
Space Complexity: $O(1)$
In [43]:
Copied!
def selection_sort(arr):
for i in range(len(arr) - 1):
min_idx = i
for j in range(i, len(arr)):
if arr[min_idx] > arr[j]:
min_idx = j
arr[i], arr[min_idx] = arr[min_idx], arr[i]
return arr
def selection_sort(arr):
for i in range(len(arr) - 1):
min_idx = i
for j in range(i, len(arr)):
if arr[min_idx] > arr[j]:
min_idx = j
arr[i], arr[min_idx] = arr[min_idx], arr[i]
return arr
In [44]:
Copied!
arr = [64, 25, 12, 22, 11]
arr = [64, 25, 12, 22, 11]
In [45]:
Copied!
selection_sort(arr)
selection_sort(arr)
Out[45]:
[11, 12, 22, 25, 64]
In [ ]:
Copied!
In [ ]:
Copied!