-
Notifications
You must be signed in to change notification settings - Fork 1
/
Analysis-exercise1.py
90 lines (73 loc) · 2.25 KB
/
Analysis-exercise1.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
from __future__ import print_function
import random
import time
def main():
maxN = 5
n = list()
for i in range(maxN):
n.append(10**i)
print(n)
for j in n:
a_list = random_list(j)
for k in range(5):
start1 = time.time()
O_nsquare_sort(a_list)
end1 = time.time()
time1 = (end1 - start1)
for k in range(5):
start2 = time.time()
O_n_sort(a_list)
end2 = time.time()
time2 = (end2-start2)
print("N = ", "%6d" %j, "\t", "selection sort time = ", "%1.6f" %time1, "\t", "radix sort time = ", "%1.6f" %time2)
def O_nsquare_sort(a_list):
""" selection sort """
b_list = list()
copy_a = a_list[:]
while len(copy_a)!=0:
min_position = 0
lmin = copy_a[0]
for i in range(len(copy_a)):
if copy_a[i] < lmin:
lmin = copy_a[i]
min_position = i
copy_a.pop(min_position)
b_list.append(lmin)
return b_list
def O_n_sort(a_list, base=10):
""" a simple immplementation of radix sort
source: http://en.wikipedia.org/wiki/Radix_sort#Example_in_Python
"""
def list_to_buckets(a_list, base, iteration):
buckets = [[] for _ in range(base)]
for number in a_list:
# Isolate the base-digit from the number
digit = ( number // (base ** iteration) ) % base
# Drop the number into the correct bucket
buckets[digit].append(number)
return buckets
def buckets_to_list(buckets):
array = []
# Collapse buckets back into a list
for bucket in buckets:
array.extend(bucket)
return array
# Find the largest value in the a_list to
maxval = 0
for i in a_list:
if i > maxval:
maxval = i
it = 0
# Iterate, sorting the a_list by each base-digit
while ( (base ** it) <= maxval):
a_list = buckets_to_list(list_to_buckets(a_list, base, it))
it += 1
return a_list
def random_list(n):
"""randomly generate a list of n numbers """
a_list = list()
for i in range(n):
a_list.append(random.randint(0, 100000))
return a_list
if __name__=="__main__":
main()