-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelection Sort.py
More file actions
55 lines (45 loc) · 1.25 KB
/
Selection Sort.py
File metadata and controls
55 lines (45 loc) · 1.25 KB
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
51
52
53
54
55
import random
import time
array = []
for i in range(0,10000):
array.append(int(round(random.random()*1000000)))
print array
startTime1 = time.time()
def selectionSortSlow(arr):
sortNum = 0
length = len(arr)
while sortNum < length:
Min = sortNum
for i in range(sortNum,length):
if (arr[Min] > arr[i]):
Min = i
temp = arr[Min]
arr[Min] = arr[sortNum]
arr[sortNum] = temp
sortNum += 1
print arr
selectionSortSlow(array)
endTime1 = time.time()
startTime2 = time.time()
def selectionSortFast(arr):
sortNum = 0
length = len(arr)/2+1
while sortNum < length:
Min = sortNum
Max = sortNum
for i in range(sortNum,length):
if (arr[Min] > arr[i]):
Min = i
if (arr[Max] < arr[i]):
Max = i
temp = arr[Min]
arr[Min] = arr[sortNum]
arr[sortNum] = temp
temp = arr[Max]
arr[Max] = arr[length-sortNum-1]
arr[length-sortNum-1] = temp
sortNum += 1
print arr
selectionSortFast(array)
endTime2 = time.time()
print ("Slow Selection Sort took: %s seconds. Fast Selction Sort took: %s seconds." %(endTime1 - startTime1, endTime2 - startTime2))