Skip to content

Commit 5c3f892

Browse files
Added selection sort code in python
1 parent 2607be0 commit 5c3f892

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

SelectionSort.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Selection sort is a sorting function that finds the minimum element
2+
# from the unsorted part of the array(i.e right) and moves it to the
3+
# sorted part the array(i.e left)
4+
5+
def selectionSort(arr):
6+
for i in range(len(arr)-1):
7+
#initially we take i as index that has the minimum value
8+
min = i
9+
#then we search the part of the array after this index
10+
for j in range(i+1, len(arr)):
11+
#if we find an index whose value is less than that at min then
12+
if arr[j]<arr[min]:
13+
#we assign this index as the index that has the minimum value
14+
min = j
15+
#after the this loop we place the element at min index at its respective position
16+
arr[min], arr[i] = arr[i], arr[min]
17+
18+
A = list(map(int, input('Please enter your array:\n').split())) #enter array as space separated integers
19+
print('\nThe array after selection sort is: \n')
20+
selectionSort(A)
21+
print(A)
22+

0 commit comments

Comments
 (0)