Skip to content

Commit 401797e

Browse files
committed
added selection sort in sorting techniques in /python/sorting-techniques
1 parent 7bb6ed9 commit 401797e

File tree

1 file changed

+24
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)