-
Notifications
You must be signed in to change notification settings - Fork 0
/
SelectionSort.py
50 lines (35 loc) · 1.36 KB
/
SelectionSort.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
38
39
40
41
42
43
44
45
46
47
48
49
50
def selection_sort(array: list) -> list:
"""Selection Sort Algorithm
The selection sort algorithm sorts an array by repeatedly finding the minimum element (considering
ascending order) from unsorted part and putting it at the beginning. The algorithm maintains two
subarrays in a given array.
1) The subarray which is already sorted.
2) Remaining subarray which is unsorted.
In every iteration of selection sort, the minimum element (considering ascending order) from the
unsorted subarray is picked and moved to the sorted subarray.
Following example explains the above steps:
arr[] = 64 25 12 22 11
Find the minimum element in arr[0...4] and place it at beginning
11 25 12 22 64
Find the minimum element in arr[1...4] and place it at beginning of arr[1...4]
11 12 25 22 64
Find the minimum element in arr[2...4] and place it at beginning of arr[2...4]
11 12 22 25 64
Find the minimum element in arr[3...4] and place it at beginning of arr[3...4]
11 12 22 25 64
"""
array1 = []
while True:
min = array[0]
for j in array:
if j < min:
min = j
array1.append(min)
array.remove(min)
if len(array) == 0:
break
del array
array = array1.copy()
del array1
del min
return array