- Notifications
You must be signed in to change notification settings - Fork 638
Expand file tree
/
Copy pathP24_SelectionSort.py
More file actions
Latest commit
21 lines (17 loc) · 713 Bytes
/
Copy pathP24_SelectionSort.py
File metadata and controls
21 lines (17 loc) · 713 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#Author: OMKAR PATHAK
#This program shows an example of selection sort
#Selection sort iterates all the elements and if the smallest element in the list is found then that number
#is swapped with the first
#Best O(n^2); Average O(n^2); Worst O(n^2)
defselectionSort(List):
foriinrange(len(List) -1): #For iterating n - 1 times
minimum=i
forjinrange( i+1, len(List)): # Compare i and i + 1 element
if(List[j] <List[minimum]):
minimum=j
if(minimum!=i):
List[i], List[minimum] =List[minimum], List[i]
returnList
if__name__=='__main__':
List= [3, 4, 2, 6, 5, 7, 1, 9]
print('Sorted List:',selectionSort(List))