Skip to content

Commit 1ca162d

Browse files
anujpatel03techyminati
authored andcommitted
Create selection_sort.py
1 parent 20b8b98 commit 1ca162d

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

‎selection_sort.py‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Selection sort in Python
2+
# time complexity O(n*n)
3+
#sorting by finding min_index
4+
def selectionSort(array, size):
5+
6+
for ind in range(size):
7+
min_index = ind
8+
9+
for j in range(ind + 1, size):
10+
# select the minimum element in every iteration
11+
if array[j] < array[min_index]:
12+
min_index = j
13+
# swapping the elements to sort the array
14+
(array[ind], array[min_index]) = (array[min_index], array[ind])
15+
16+
arr = [-12, 45, 10, 11, -19,68,-96,-22,77]
17+
size = len(arr)
18+
selectionSort(arr, size)
19+
print('The array after sorting in Ascending Order by selection sort is:')
20+
print(arr)

0 commit comments

Comments
 (0)