Skip to content

Commit 6372ed2

Browse files
Merge pull request #429 from ghubrakesh/main
#78 added selection sort
2 parents 3b2a621 + 9405a8a commit 6372ed2

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

Python/Sorting-Techniques/selectionSort.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,26 @@
1+
2+
# program for selection sort
3+
#selections sort takes O(n^2) time complexity
4+
5+
6+
7+
#program starts from here
8+
def selectionSort( itemsList ):
9+
n = len( itemsList )
10+
for i in range( n - 1 ):
11+
minValueIndex = i
12+
13+
for j in range( i + 1, n ):
14+
if itemsList[j] < itemsList[minValueIndex] :
15+
minValueIndex = j
16+
17+
if minValueIndex != i :
18+
temp = itemsList[i]
19+
itemsList[i] = itemsList[minValueIndex]
20+
itemsList[minValueIndex] = temp
21+
22+
return itemsList
23+
124
# Selection sort in Python
225

326
# time complexity O(n^2)
@@ -21,3 +44,4 @@ def selectionSort(array, size):
2144
selectionSort(arr, size)
2245
print('The array after sorting in Ascending Order by selection sort is:')
2346
print(arr)
47+

0 commit comments

Comments
 (0)