- Notifications
You must be signed in to change notification settings - Fork 366
/
Copy pathselection_sort.py
24 lines (18 loc) · 651 Bytes
/
selection_sort.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# Selection sort in Python
# time complexity O(n^2)
#sorting by finding min_index
#CODE =
defselectionSort(array, size):
forindinrange(size):
min_index=ind
forjinrange(ind+1, size):
# select the minimum element in every iteration
ifarray[j] <array[min_index]:
min_index=j
# swapping the elements to sort the array
(array[ind], array[min_index]) = (array[min_index], array[ind])
arr= [-2, 45, 0, 11, -9,88,-97,-202,747]
size=len(arr)
selectionSort(arr, size)
print('The array after sorting in Ascending Order by selection sort is:')
print(arr)