-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelectionSort.py
More file actions
executable file
·39 lines (34 loc) · 847 Bytes
/
SelectionSort.py
File metadata and controls
executable file
·39 lines (34 loc) · 847 Bytes
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
#!/usr/bin/env python
def SelectionSort(arr):
# back array
size = len(arr)
i = 0
while i < size - 1:
maxIndex = 0
j = 1
while j < size - 1 - i:
if arr[j] > arr[maxIndex]:
maxIndex = j
j += 1
temp = arr[size - 1 - i]
arr[size - 1 - i] = arr[maxIndex]
arr[maxIndex] = temp
i += 1
def SelectionSort2(arr):
# front array
size = len(arr)
i = 0
while i < size - 1:
minIndex = i
j = i + 1
while j < size:
if arr[j] < arr[minIndex]:
minIndex = j
j += 1
temp = arr[i]
arr[i] = arr[minIndex]
arr[minIndex] = temp
i += 1
array = [9, 1, 8, 2, 7, 3, 6, 4, 5]
SelectionSort(array)
print array