-
Notifications
You must be signed in to change notification settings - Fork 0
/
selection_sort.py
37 lines (30 loc) · 1.13 KB
/
selection_sort.py
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
34
35
36
37
# Selection sort algorithm that I translated from Javascript into Python while reading the book Common Sense Guide to Data Structures and Algorithms by Jay Wengrow, as a means to cross-train my brain to think in both languages.
def selection_sort(array):
for i in range(len(array)):
lowestNumberIndex = i
for j in range(i + 1, len(array)):
if array[j] < array[lowestNumberIndex]:
lowestNumberIndex = j
array[i], array[lowestNumberIndex] = array[lowestNumberIndex], array[i]
return array
sorted_array = selection_sort([5, 3, 6, 2, 10])
print(sorted_array)
# Output: [2, 3, 5, 6, 10]
# Time complexity: O(n^2)
#The original source code for this algorithm in Javascript
# function selectionSort(array) {
# for (let i = 0; i < array.length - 1; i++) {
# let lowestNumberIndex = i;
# for (let j = i + 1; j < array.length; j++) {
# if (array[j] < array[lowestNumberIndex]) {
# lowestNumberIndex = j;
# }
# }
# if (lowestNumberIndex != i) {
# let temp = array[i];
# array[i] = array[lowestNumberIndex];
# array[lowestNumberIndex] = temp;
# }
# }
# return array;
# }