Skip to content

Commit aef3f37

Browse files
committed
learn sort,add timer
1 parent 101cccd commit aef3f37

File tree

4 files changed

+34
-15
lines changed

4 files changed

+34
-15
lines changed

sorts/bogosort.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def isSorted(collection):
4040

4141
if __name__ == '__main__':
4242
import sys
43-
43+
'''
4444
# For python 2.x and 3.x compatibility: 3.x has not raw_input builtin
4545
# otherwise 2.x's input builtin function is too "smart"
4646
if sys.version_info.major < 3:
@@ -50,4 +50,6 @@ def isSorted(collection):
5050
5151
user_input = input_function('Enter numbers separated by a comma:\n')
5252
unsorted = [int(item) for item in user_input.split(',')]
53+
'''
54+
unsorted=list(range(10,1,-1))
5355
print(bogosort(unsorted))

sorts/bubble_sort.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"""
1212

1313
from __future__ import print_function
14-
14+
import time
1515

1616
def bubble_sort(collection):
1717
"""Pure implementation of bubble sort algorithm in Python
@@ -41,6 +41,7 @@ def bubble_sort(collection):
4141

4242
if __name__ == '__main__':
4343
import sys
44+
'''
4445
# For python 2.x and 3.x compatibility: 3.x has not raw_input builtin
4546
# otherwise 2.x's input builtin function is too "smart"
4647
if sys.version_info.major < 3:
@@ -50,4 +51,9 @@ def bubble_sort(collection):
5051
5152
user_input = input_function('Enter numbers separated by a comma:\n')
5253
unsorted = [int(item) for item in user_input.split(',')]
54+
'''
55+
tstart=time.clock()
56+
unsorted=list(range(5000,1,-1))
5357
print(bubble_sort(unsorted))
58+
tend=time.clock()
59+
print("read: %f s" % (tend - tstart))

sorts/heap_sort.py

+15-9
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,18 @@ def heap_sort(unsorted):
5454
return unsorted
5555

5656
if __name__ == '__main__':
57-
import sys
58-
if sys.version_info.major < 3:
59-
input_function = raw_input
60-
else:
61-
input_function = input
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(heap_sort(unsorted))
57+
import sys
58+
import time
59+
if sys.version_info.major < 3:
60+
input_function = raw_input
61+
else:
62+
input_function = input
63+
tstart=time.clock()
64+
unsorted=list(range(150000,1,-1))
65+
#print(sort(unsorted))
66+
print(heap_sort(unsorted))
67+
tend=time.clock()
68+
print("read: %f s" % (tend - tstart))
69+
#user_input = input_function('Enter numbers separated by a comma:\n')
70+
#unsorted = [int(item) for item in user_input.split(',')]
71+
#print(heap_sort(unsorted))

sorts/quick_sort.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"""
1212
from __future__ import print_function
1313
from random import shuffle
14-
14+
import time
1515

1616
def sort(collection):
1717
shuffle(collection)
@@ -61,6 +61,11 @@ def quick_sort(collection):
6161
else:
6262
input_function = input
6363

64-
user_input = input_function('Enter numbers separated by a comma:\n')
65-
unsorted = [int(item) for item in user_input.split(',')]
66-
print(sort(unsorted))
64+
#user_input = input_function('Enter numbers separated by a comma:\n')
65+
#unsorted = [int(item) for item in user_input.split(',')]
66+
tstart=time.clock()
67+
unsorted=list(range(500,1,-1))
68+
#print(sort(unsorted))
69+
print(quick_sort(unsorted))
70+
tend=time.clock()
71+
print("read: %f s" % (tend - tstart))

0 commit comments

Comments
 (0)