-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path33selection-sort.py
More file actions
28 lines (26 loc) · 924 Bytes
/
33selection-sort.py
File metadata and controls
28 lines (26 loc) · 924 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
'''
selection sort algorithm
- Number of comparisons: O(n^2)
- Number of swaps: O(n)
- A minimum element is selected from the unsorted part and swapped with the first unsorted element.
- Bubble sort performs multiple swaps, while selection sort performs only one swap per pass.
- Time complexity: O(n^2)
- Space complexity: O(1)
'''
def selection_sort(arr):
n = len(arr)
for i in range(n):
# Assume the minimum is the first element of the unsorted part
min_index = i
for j in range(i + 1, n):
if arr[j] < arr[min_index]:
min_index = j
# Swap the found minimum element with the first unsorted element
arr[i], arr[min_index] = arr[min_index], arr[i]
return arr
# Example usage
if __name__ == "__main__":
arr = [64, 25, 12, 22, 11]
print("Original array:", arr)
sorted_arr = selection_sort(arr)
print("Sorted array:", sorted_arr)