Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 7a451a1

Browse files
authoredOct 19, 2024··
Selection Sort
1 parent 03a4251 commit 7a451a1

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
 

‎sorts/Selection Sort

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
def selectionSort(array, size):
2+
3+
for s in range(size):
4+
min_idx = s
5+
6+
for i in range(s + 1, size):
7+
8+
# For sorting in descending order
9+
# for minimum element in each loop
10+
if array[i] < array[min_idx]:
11+
min_idx = i
12+
13+
# Arranging min at the correct position
14+
(array[s], array[min_idx]) = (array[min_idx], array[s])
15+
16+
data = [ 7, 2, 1, 6 ]
17+
size = len(data)
18+
selectionSort(data, size)
19+
20+
print('Sorted Array in Ascending Order is :')
21+
print(data)

0 commit comments

Comments
 (0)
Please sign in to comment.