Skip to content

Commit 24edd89

Browse files
authored
Selection Sort in Python (#249)
Selection Sort in Python
2 parents c8cd7c6 + f082b8d commit 24edd89

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

Sorting/selection sort.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#Sorting a list in Ascending Order using Selection Sort
2+
3+
#Selection sort function
4+
def selection_sort(alist):
5+
for i in range(0, len(alist) - 1):
6+
small = i
7+
for j in range(i + 1, len(alist)):
8+
if alist[j] < alist[small]:
9+
small = j
10+
alist[i], alist[small] = alist[small], alist[i]
11+
12+
# Taking a list of space seperated numbers from user
13+
alist = input('Enter the list of numbers: ').split()
14+
alist = [int(x) for x in alist]
15+
selection_sort(alist)
16+
print('Sorted list: ', end='')
17+
print(alist)

0 commit comments

Comments
 (0)