Skip to content

Commit c677900

Browse files
committed
add test
1 parent 20de6d3 commit c677900

File tree

6 files changed

+135
-86
lines changed

6 files changed

+135
-86
lines changed

sorts/bubble_sort.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def bubble_sort(collection):
5353
unsorted = [int(item) for item in user_input.split(',')]
5454
'''
5555
tstart=time.clock()
56-
unsorted=list(range(5000,1,-1))
56+
unsorted=list(range(10000,1,-1))
5757
print(bubble_sort(unsorted))
5858
tend=time.clock()
5959
print("read: %f s" % (tend - tstart))

sorts/heap_sort.py

+11-8
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
import pdb
1616
def heapify(unsorted, index, heap_size):
17-
print("in heapify: ", "index=",index," list=",unsorted)
17+
#print("in heapify: ", "index=",index," list=",unsorted)
1818

1919
largest = index
2020
left_index = 2 * index + 1
@@ -48,7 +48,7 @@ def heap_sort(unsorted):
4848
>>> heap_sort([-2, -5, -45])
4949
[-45, -5, -2]
5050
'''
51-
print("in heap_sort: ", " list=",unsorted)
51+
#print("in heap_sort: ", " list=",unsorted)
5252
n = len(unsorted)
5353
for i in range(n // 2 - 1, -1, -1):
5454
heapify(unsorted, i, n)
@@ -66,12 +66,15 @@ def heap_sort(unsorted):
6666
else:
6767
input_function = input
6868
tstart=time.clock()
69-
pdb.set_trace()
70-
unsorted=list(range(10,1,-1))
71-
#print(sort(unsorted))
72-
print(heap_sort(unsorted))
73-
tend=time.clock()
74-
print("read: %f s" % (tend - tstart))
69+
#pdb.set_trace()
70+
unsorted=list(range(10000000,1,-1))
71+
#print(sort(unsorted))
72+
heap_sort(unsorted)
73+
#print(heap_sort(unsorted))
74+
tend=time.clock()
75+
print("read: %f s" % (tend - tstart))
7576
#user_input = input_function('Enter numbers separated by a comma:\n')
7677
#unsorted = [int(item) for item in user_input.split(',')]
7778
#print(heap_sort(unsorted))
79+
80+

sorts/insertion_sort.py

+19-12
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,22 @@ def insertion_sort(collection):
3939

4040

4141
if __name__ == '__main__':
42-
import sys
43-
44-
# For python 2.x and 3.x compatibility: 3.x has not raw_input builtin
45-
# otherwise 2.x's input builtin function is too "smart"
46-
if sys.version_info.major < 3:
47-
input_function = raw_input
48-
else:
49-
input_function = input
50-
51-
user_input = input_function('Enter numbers separated by a comma:\n')
52-
unsorted = [int(item) for item in user_input.split(',')]
53-
print(insertion_sort(unsorted))
42+
import sys
43+
44+
# For python 2.x and 3.x compatibility: 3.x has not raw_input builtin
45+
# otherwise 2.x's input builtin function is too "smart"
46+
if sys.version_info.major < 3:
47+
input_function = raw_input
48+
else:
49+
input_function = input
50+
import time
51+
tstart=time.clock()
52+
#pdb.set_trace()
53+
unsorted=list(range(10000,1,-1))
54+
#print(sort(unsorted))
55+
print(insertion_sort(unsorted))
56+
tend=time.clock()
57+
print("read: %f s" % (tend - tstart))
58+
#user_input = input_function('Enter numbers separated by a comma:\n')
59+
#unsorted = [int(item) for item in user_input.split(',')]
60+
#print(insertion_sort(unsorted))

sorts/merge_sort.py

+29-13
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,14 @@ def merge_sort(collection):
2929
>>> merge_sort([-2, -5, -45])
3030
[-45, -5, -2]
3131
"""
32+
#print("collection=",collection)
3233
length = len(collection)
3334
if length > 1:
3435
midpoint = length // 2
3536
left_half = merge_sort(collection[:midpoint])
36-
right_half = merge_sort(collection[midpoint:])
37+
#print("left_half= ",left_half)
38+
right_half = merge_sort(collection[midpoint:])
39+
#print("right_half= ",right_half)
3740
i = 0
3841
j = 0
3942
k = 0
@@ -62,15 +65,28 @@ def merge_sort(collection):
6265

6366

6467
if __name__ == '__main__':
65-
import sys
66-
67-
# For python 2.x and 3.x compatibility: 3.x has not raw_input builtin
68-
# otherwise 2.x's input builtin function is too "smart"
69-
if sys.version_info.major < 3:
70-
input_function = raw_input
71-
else:
72-
input_function = input
73-
74-
user_input = input_function('Enter numbers separated by a comma:\n')
75-
unsorted = [int(item) for item in user_input.split(',')]
76-
print(merge_sort(unsorted))
68+
import sys
69+
70+
# For python 2.x and 3.x compatibility: 3.x has not raw_input builtin
71+
# otherwise 2.x's input builtin function is too "smart"
72+
if sys.version_info.major < 3:
73+
input_function = raw_input
74+
else:
75+
input_function = input
76+
77+
import time
78+
tstart=time.clock()
79+
#pdb.set_trace()
80+
unsorted=list(range(10000000,1,-1))
81+
#print(sort(unsorted))
82+
#print(merge_sort(unsorted))
83+
merge_sort(unsorted)
84+
tend=time.clock()
85+
print("read: %f s" % (tend - tstart))
86+
#user_input = input_function('Enter numbers separated by a comma:\n')
87+
#unsorted = [int(item) for item in user_input.split(',')]
88+
#print(merge_sort(unsorted))
89+
90+
91+
92+

sorts/selection_sort.py

+24-11
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,27 @@ def selection_sort(collection):
4343

4444

4545
if __name__ == '__main__':
46-
import sys
47-
# For python 2.x and 3.x compatibility: 3.x has not raw_input builtin
48-
# otherwise 2.x's input builtin function is too "smart"
49-
if sys.version_info.major < 3:
50-
input_function = raw_input
51-
else:
52-
input_function = input
53-
54-
user_input = input_function('Enter numbers separated by a comma:\n')
55-
unsorted = [int(item) for item in user_input.split(',')]
56-
print(selection_sort(unsorted))
46+
import sys
47+
# For python 2.x and 3.x compatibility: 3.x has not raw_input builtin
48+
# otherwise 2.x's input builtin function is too "smart"
49+
if sys.version_info.major < 3:
50+
input_function = raw_input
51+
else:
52+
input_function = input
53+
54+
import time
55+
tstart=time.clock()
56+
#pdb.set_trace()
57+
unsorted=list(range(20000,1,-1))
58+
#print(sort(unsorted))
59+
print(selection_sort(unsorted))
60+
tend=time.clock()
61+
print("read: %f s" % (tend - tstart))
62+
63+
#user_input = input_function('Enter numbers separated by a comma:\n')
64+
#unsorted = [int(item) for item in user_input.split(',')]
65+
#print(selection_sort(unsorted))
66+
67+
68+
69+

sorts/shell_sort.py

+51-41
Original file line numberDiff line numberDiff line change
@@ -13,45 +13,55 @@
1313

1414

1515
def shell_sort(collection):
16-
"""Pure implementation of shell sort algorithm in Python
17-
:param collection: Some mutable ordered collection with heterogeneous
18-
comparable items inside
19-
:return: the same collection ordered by ascending
20-
21-
>>> shell_sort([0, 5, 3, 2, 2])
22-
[0, 2, 2, 3, 5]
23-
24-
>>> shell_sort([])
25-
[]
26-
27-
>>> shell_sort([-2, -5, -45])
28-
[-45, -5, -2]
29-
"""
30-
# Marcin Ciura's gap sequence
31-
gaps = [701, 301, 132, 57, 23, 10, 4, 1]
32-
33-
for gap in gaps:
34-
i = gap
35-
while i < len(collection):
36-
temp = collection[i]
37-
j = i
38-
while j >= gap and collection[j - gap] > temp:
39-
collection[j] = collection[j - gap]
40-
j -= gap
41-
collection[j] = temp
42-
i += 1
43-
44-
return collection
45-
16+
"""Pure implementation of shell sort algorithm in Python
17+
:param collection: Some mutable ordered collection with heterogeneous
18+
comparable items inside
19+
:return: the same collection ordered by ascending
20+
21+
>>> shell_sort([0, 5, 3, 2, 2])
22+
[0, 2, 2, 3, 5]
23+
24+
>>> shell_sort([])
25+
[]
26+
27+
>>> shell_sort([-2, -5, -45])
28+
[-45, -5, -2]
29+
"""
30+
# Marcin Ciura's gap sequence
31+
gaps = [701, 301, 132, 57, 23, 10, 4, 1]
32+
33+
for gap in gaps:
34+
i = gap
35+
while i < len(collection):
36+
temp = collection[i]
37+
j = i
38+
print("****")
39+
while j >= gap and collection[j - gap] > temp:
40+
print("----------------------> ",j)
41+
collection[j] = collection[j - gap]
42+
j -= gap
43+
collection[j] = temp
44+
i += 1
45+
46+
return collection
47+
4648
if __name__ == '__main__':
47-
import sys
48-
# For python 2.x and 3.x compatibility: 3.x has not raw_input builtin
49-
# otherwise 2.x's input builtin function is too "smart"
50-
if sys.version_info.major < 3:
51-
input_function = raw_input
52-
else:
53-
input_function = input
54-
55-
user_input = input_function('Enter numbers separated by a comma:\n')
56-
unsorted = [int(item) for item in user_input.split(',')]
57-
print(shell_sort(unsorted))
49+
import sys
50+
# For python 2.x and 3.x compatibility: 3.x has not raw_input builtin
51+
# otherwise 2.x's input builtin function is too "smart"
52+
if sys.version_info.major < 3:
53+
input_function = raw_input
54+
else:
55+
input_function = input
56+
import time
57+
tstart=time.clock()
58+
#pdb.set_trace()
59+
unsorted=list(range(8000,1,-1))
60+
#print(sort(unsorted))
61+
print(shell_sort(unsorted))
62+
tend=time.clock()
63+
print("read: %f s" % (tend - tstart))
64+
65+
#user_input = input_function('Enter numbers separated by a comma:\n')
66+
#unsorted = [int(item) for item in user_input.split(',')]
67+
#print(shell_sort(unsorted))

0 commit comments

Comments
 (0)