generated from ComputerScientist-01/Readme_template
-
Notifications
You must be signed in to change notification settings - Fork 4
/
selectionSort.py
37 lines (24 loc) · 839 Bytes
/
selectionSort.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
25
26
27
28
29
30
31
32
33
34
35
36
# Selection sort in Python
def selectionSort(array):
# keep track of swapping
swapped = False
# loop through each element of array
for i in range(len(array)):
# for keeping the smallest integer
index = i
# loop to compare array elements
for j in range(i+1,len(array)):
# change > to < to sort in descending order
if array[j]<array[index]:
# index is equal to the condition number
index=j
swapped=True
# swapping occurs if elements
# are not in the intended order
temp=array[i]
array[i]=array[index]
array[index]=temp
# no swapping means the array is already sorted
# so no need for further comparison
if not swapped:
break