forked from DHEERAJHARODE/Hacktoberfest2024-Open-source-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSelectionSortInPython
33 lines (25 loc) · 983 Bytes
/
SelectionSortInPython
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/*
* selection sort algorithm is idea of finding the smallest unsorted element and add it to the end of the sorted list.
* selection sort is of complexity O(n^2) and omega(n^2)
// -- PSEUDO CODE --
// Repeat until no unsorted elements remain:
// Search the unsorted part of data to find the smallest value.
// Swap the smallest value found with the first element of unsorted part.
*/
def swap(array, firstIndex, secondIndex):
temp = array[firstIndex]
array[firstIndex] = array[secondIndex]
array[secondIndex] = temp
def indexOfMinimum(array, startIndex):
minValue = array[startIndex]
minIndex = startIndex
for i in xrange(minIndex + 1, len(array)):
if array[i] < minValue:
minIndex = i
minValue = array[i]
return minIndex
def selectionSort(array):
for currentPos in range(0, len(array)):
minIndex = indexOfMinimum(array, currentPos)
swap(array, currentPos, minIndex)
return