From aef3f377cc4a560492efde6621d532145dabe510 Mon Sep 17 00:00:00 2001 From: renwl Date: Thu, 13 Oct 2016 22:16:57 +0800 Subject: [PATCH 1/3] learn sort,add timer --- sorts/bogosort.py | 4 +++- sorts/bubble_sort.py | 8 +++++++- sorts/heap_sort.py | 24 +++++++++++++++--------- sorts/quick_sort.py | 13 +++++++++---- 4 files changed, 34 insertions(+), 15 deletions(-) diff --git a/sorts/bogosort.py b/sorts/bogosort.py index 2512dab51761..0e4d571428c8 100644 --- a/sorts/bogosort.py +++ b/sorts/bogosort.py @@ -40,7 +40,7 @@ def isSorted(collection): if __name__ == '__main__': import sys - + ''' # For python 2.x and 3.x compatibility: 3.x has not raw_input builtin # otherwise 2.x's input builtin function is too "smart" if sys.version_info.major < 3: @@ -50,4 +50,6 @@ def isSorted(collection): user_input = input_function('Enter numbers separated by a comma:\n') unsorted = [int(item) for item in user_input.split(',')] + ''' + unsorted=list(range(10,1,-1)) print(bogosort(unsorted)) diff --git a/sorts/bubble_sort.py b/sorts/bubble_sort.py index 54d69e5ba389..df8301d4f7e6 100644 --- a/sorts/bubble_sort.py +++ b/sorts/bubble_sort.py @@ -11,7 +11,7 @@ """ from __future__ import print_function - +import time def bubble_sort(collection): """Pure implementation of bubble sort algorithm in Python @@ -41,6 +41,7 @@ def bubble_sort(collection): if __name__ == '__main__': import sys + ''' # For python 2.x and 3.x compatibility: 3.x has not raw_input builtin # otherwise 2.x's input builtin function is too "smart" if sys.version_info.major < 3: @@ -50,4 +51,9 @@ def bubble_sort(collection): user_input = input_function('Enter numbers separated by a comma:\n') unsorted = [int(item) for item in user_input.split(',')] + ''' + tstart=time.clock() + unsorted=list(range(5000,1,-1)) print(bubble_sort(unsorted)) + tend=time.clock() + print("read: %f s" % (tend - tstart)) diff --git a/sorts/heap_sort.py b/sorts/heap_sort.py index 2d9dd844d0fa..3a9291e15102 100644 --- a/sorts/heap_sort.py +++ b/sorts/heap_sort.py @@ -54,12 +54,18 @@ def heap_sort(unsorted): return unsorted if __name__ == '__main__': - import sys - if sys.version_info.major < 3: - input_function = raw_input - else: - input_function = input - - user_input = input_function('Enter numbers separated by a comma:\n') - unsorted = [int(item) for item in user_input.split(',')] - print(heap_sort(unsorted)) + import sys + import time + if sys.version_info.major < 3: + input_function = raw_input + else: + input_function = input + tstart=time.clock() + unsorted=list(range(150000,1,-1)) + #print(sort(unsorted)) + print(heap_sort(unsorted)) + tend=time.clock() + print("read: %f s" % (tend - tstart)) + #user_input = input_function('Enter numbers separated by a comma:\n') + #unsorted = [int(item) for item in user_input.split(',')] + #print(heap_sort(unsorted)) diff --git a/sorts/quick_sort.py b/sorts/quick_sort.py index 257c21380dce..76dfc2d8b88c 100644 --- a/sorts/quick_sort.py +++ b/sorts/quick_sort.py @@ -11,7 +11,7 @@ """ from __future__ import print_function from random import shuffle - +import time def sort(collection): shuffle(collection) @@ -61,6 +61,11 @@ def quick_sort(collection): else: input_function = input - user_input = input_function('Enter numbers separated by a comma:\n') - unsorted = [int(item) for item in user_input.split(',')] - print(sort(unsorted)) + #user_input = input_function('Enter numbers separated by a comma:\n') + #unsorted = [int(item) for item in user_input.split(',')] + tstart=time.clock() + unsorted=list(range(500,1,-1)) + #print(sort(unsorted)) + print(quick_sort(unsorted)) + tend=time.clock() + print("read: %f s" % (tend - tstart)) From 20de6d32d269e7d4ea1d86b7e574b94739da1c77 Mon Sep 17 00:00:00 2001 From: renwl Date: Fri, 14 Oct 2016 12:22:31 +0800 Subject: [PATCH 2/3] add pdb in code --- sorts/heap_sort.py | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/sorts/heap_sort.py b/sorts/heap_sort.py index 3a9291e15102..0b4768daed88 100644 --- a/sorts/heap_sort.py +++ b/sorts/heap_sort.py @@ -12,22 +12,25 @@ from __future__ import print_function - +import pdb def heapify(unsorted, index, heap_size): - largest = index - left_index = 2 * index + 1 - right_index = 2 * index + 2 - if left_index < heap_size and unsorted[left_index] > unsorted[largest]: - largest = left_index - - if right_index < heap_size and unsorted[right_index] > unsorted[largest]: - largest = right_index - - if largest != index: - unsorted[largest], unsorted[index] = unsorted[index], unsorted[largest] - heapify(unsorted, largest, heap_size) - - + print("in heapify: ", "index=",index," list=",unsorted) + + largest = index + left_index = 2 * index + 1 + right_index = 2 * index + 2 + if left_index < heap_size and unsorted[left_index] > unsorted[largest]: + largest = left_index + + if right_index < heap_size and unsorted[right_index] > unsorted[largest]: + largest = right_index + + if largest != index: + unsorted[largest], unsorted[index] = unsorted[index], unsorted[largest] + #print("in heapify: ",unsorted) + heapify(unsorted, largest, heap_size) + + def heap_sort(unsorted): ''' Pure implementation of the heap sort algorithm in Python @@ -45,11 +48,13 @@ def heap_sort(unsorted): >>> heap_sort([-2, -5, -45]) [-45, -5, -2] ''' + print("in heap_sort: ", " list=",unsorted) n = len(unsorted) for i in range(n // 2 - 1, -1, -1): heapify(unsorted, i, n) for i in range(n - 1, 0, -1): unsorted[0], unsorted[i] = unsorted[i], unsorted[0] + #print("in heap_sort: ",unsorted) heapify(unsorted, 0, i) return unsorted @@ -61,7 +66,8 @@ def heap_sort(unsorted): else: input_function = input tstart=time.clock() - unsorted=list(range(150000,1,-1)) + pdb.set_trace() + unsorted=list(range(10,1,-1)) #print(sort(unsorted)) print(heap_sort(unsorted)) tend=time.clock() From c677900148676a691ab3ea6a5f03c15e64fc4aef Mon Sep 17 00:00:00 2001 From: renwl Date: Mon, 17 Oct 2016 12:58:07 +0800 Subject: [PATCH 3/3] add test --- sorts/bubble_sort.py | 2 +- sorts/heap_sort.py | 19 +++++---- sorts/insertion_sort.py | 31 ++++++++------ sorts/merge_sort.py | 42 +++++++++++++------ sorts/selection_sort.py | 35 +++++++++++----- sorts/shell_sort.py | 92 +++++++++++++++++++++++------------------ 6 files changed, 135 insertions(+), 86 deletions(-) diff --git a/sorts/bubble_sort.py b/sorts/bubble_sort.py index df8301d4f7e6..075a6a6e939f 100644 --- a/sorts/bubble_sort.py +++ b/sorts/bubble_sort.py @@ -53,7 +53,7 @@ def bubble_sort(collection): unsorted = [int(item) for item in user_input.split(',')] ''' tstart=time.clock() - unsorted=list(range(5000,1,-1)) + unsorted=list(range(10000,1,-1)) print(bubble_sort(unsorted)) tend=time.clock() print("read: %f s" % (tend - tstart)) diff --git a/sorts/heap_sort.py b/sorts/heap_sort.py index 0b4768daed88..8eb5e96a4b58 100644 --- a/sorts/heap_sort.py +++ b/sorts/heap_sort.py @@ -14,7 +14,7 @@ import pdb def heapify(unsorted, index, heap_size): - print("in heapify: ", "index=",index," list=",unsorted) + #print("in heapify: ", "index=",index," list=",unsorted) largest = index left_index = 2 * index + 1 @@ -48,7 +48,7 @@ def heap_sort(unsorted): >>> heap_sort([-2, -5, -45]) [-45, -5, -2] ''' - print("in heap_sort: ", " list=",unsorted) + #print("in heap_sort: ", " list=",unsorted) n = len(unsorted) for i in range(n // 2 - 1, -1, -1): heapify(unsorted, i, n) @@ -66,12 +66,15 @@ def heap_sort(unsorted): else: input_function = input tstart=time.clock() - pdb.set_trace() - unsorted=list(range(10,1,-1)) - #print(sort(unsorted)) - print(heap_sort(unsorted)) - tend=time.clock() - print("read: %f s" % (tend - tstart)) + #pdb.set_trace() + unsorted=list(range(10000000,1,-1)) + #print(sort(unsorted)) + heap_sort(unsorted) + #print(heap_sort(unsorted)) + tend=time.clock() + print("read: %f s" % (tend - tstart)) #user_input = input_function('Enter numbers separated by a comma:\n') #unsorted = [int(item) for item in user_input.split(',')] #print(heap_sort(unsorted)) + + \ No newline at end of file diff --git a/sorts/insertion_sort.py b/sorts/insertion_sort.py index caaa9305c968..41a8697605a6 100644 --- a/sorts/insertion_sort.py +++ b/sorts/insertion_sort.py @@ -39,15 +39,22 @@ def insertion_sort(collection): if __name__ == '__main__': - import sys - - # For python 2.x and 3.x compatibility: 3.x has not raw_input builtin - # otherwise 2.x's input builtin function is too "smart" - if sys.version_info.major < 3: - input_function = raw_input - else: - input_function = input - - user_input = input_function('Enter numbers separated by a comma:\n') - unsorted = [int(item) for item in user_input.split(',')] - print(insertion_sort(unsorted)) + import sys + + # For python 2.x and 3.x compatibility: 3.x has not raw_input builtin + # otherwise 2.x's input builtin function is too "smart" + if sys.version_info.major < 3: + input_function = raw_input + else: + input_function = input + import time + tstart=time.clock() + #pdb.set_trace() + unsorted=list(range(10000,1,-1)) + #print(sort(unsorted)) + print(insertion_sort(unsorted)) + tend=time.clock() + print("read: %f s" % (tend - tstart)) + #user_input = input_function('Enter numbers separated by a comma:\n') + #unsorted = [int(item) for item in user_input.split(',')] + #print(insertion_sort(unsorted)) diff --git a/sorts/merge_sort.py b/sorts/merge_sort.py index 92a6780165ac..ee3160949d99 100644 --- a/sorts/merge_sort.py +++ b/sorts/merge_sort.py @@ -29,11 +29,14 @@ def merge_sort(collection): >>> merge_sort([-2, -5, -45]) [-45, -5, -2] """ + #print("collection=",collection) length = len(collection) if length > 1: midpoint = length // 2 left_half = merge_sort(collection[:midpoint]) - right_half = merge_sort(collection[midpoint:]) + #print("left_half= ",left_half) + right_half = merge_sort(collection[midpoint:]) + #print("right_half= ",right_half) i = 0 j = 0 k = 0 @@ -62,15 +65,28 @@ def merge_sort(collection): if __name__ == '__main__': - import sys - - # For python 2.x and 3.x compatibility: 3.x has not raw_input builtin - # otherwise 2.x's input builtin function is too "smart" - if sys.version_info.major < 3: - input_function = raw_input - else: - input_function = input - - user_input = input_function('Enter numbers separated by a comma:\n') - unsorted = [int(item) for item in user_input.split(',')] - print(merge_sort(unsorted)) + import sys + + # For python 2.x and 3.x compatibility: 3.x has not raw_input builtin + # otherwise 2.x's input builtin function is too "smart" + if sys.version_info.major < 3: + input_function = raw_input + else: + input_function = input + + import time + tstart=time.clock() + #pdb.set_trace() + unsorted=list(range(10000000,1,-1)) + #print(sort(unsorted)) + #print(merge_sort(unsorted)) + merge_sort(unsorted) + tend=time.clock() + print("read: %f s" % (tend - tstart)) + #user_input = input_function('Enter numbers separated by a comma:\n') + #unsorted = [int(item) for item in user_input.split(',')] + #print(merge_sort(unsorted)) + + + + \ No newline at end of file diff --git a/sorts/selection_sort.py b/sorts/selection_sort.py index 14bc804637c5..54fe27edce8c 100644 --- a/sorts/selection_sort.py +++ b/sorts/selection_sort.py @@ -43,14 +43,27 @@ def selection_sort(collection): if __name__ == '__main__': - import sys - # For python 2.x and 3.x compatibility: 3.x has not raw_input builtin - # otherwise 2.x's input builtin function is too "smart" - if sys.version_info.major < 3: - input_function = raw_input - else: - input_function = input - - user_input = input_function('Enter numbers separated by a comma:\n') - unsorted = [int(item) for item in user_input.split(',')] - print(selection_sort(unsorted)) + import sys + # For python 2.x and 3.x compatibility: 3.x has not raw_input builtin + # otherwise 2.x's input builtin function is too "smart" + if sys.version_info.major < 3: + input_function = raw_input + else: + input_function = input + + import time + tstart=time.clock() + #pdb.set_trace() + unsorted=list(range(20000,1,-1)) + #print(sort(unsorted)) + print(selection_sort(unsorted)) + tend=time.clock() + print("read: %f s" % (tend - tstart)) + + #user_input = input_function('Enter numbers separated by a comma:\n') + #unsorted = [int(item) for item in user_input.split(',')] + #print(selection_sort(unsorted)) + + + + \ No newline at end of file diff --git a/sorts/shell_sort.py b/sorts/shell_sort.py index fdb98a570d9f..cf31b0e3914a 100644 --- a/sorts/shell_sort.py +++ b/sorts/shell_sort.py @@ -13,45 +13,55 @@ def shell_sort(collection): - """Pure implementation of shell sort algorithm in Python - :param collection: Some mutable ordered collection with heterogeneous - comparable items inside - :return: the same collection ordered by ascending - - >>> shell_sort([0, 5, 3, 2, 2]) - [0, 2, 2, 3, 5] - - >>> shell_sort([]) - [] - - >>> shell_sort([-2, -5, -45]) - [-45, -5, -2] - """ - # Marcin Ciura's gap sequence - gaps = [701, 301, 132, 57, 23, 10, 4, 1] - - for gap in gaps: - i = gap - while i < len(collection): - temp = collection[i] - j = i - while j >= gap and collection[j - gap] > temp: - collection[j] = collection[j - gap] - j -= gap - collection[j] = temp - i += 1 - - return collection - + """Pure implementation of shell sort algorithm in Python + :param collection: Some mutable ordered collection with heterogeneous + comparable items inside + :return: the same collection ordered by ascending + + >>> shell_sort([0, 5, 3, 2, 2]) + [0, 2, 2, 3, 5] + + >>> shell_sort([]) + [] + + >>> shell_sort([-2, -5, -45]) + [-45, -5, -2] + """ + # Marcin Ciura's gap sequence + gaps = [701, 301, 132, 57, 23, 10, 4, 1] + + for gap in gaps: + i = gap + while i < len(collection): + temp = collection[i] + j = i + print("****") + while j >= gap and collection[j - gap] > temp: + print("----------------------> ",j) + collection[j] = collection[j - gap] + j -= gap + collection[j] = temp + i += 1 + + return collection + if __name__ == '__main__': - import sys - # For python 2.x and 3.x compatibility: 3.x has not raw_input builtin - # otherwise 2.x's input builtin function is too "smart" - if sys.version_info.major < 3: - input_function = raw_input - else: - input_function = input - - user_input = input_function('Enter numbers separated by a comma:\n') - unsorted = [int(item) for item in user_input.split(',')] - print(shell_sort(unsorted)) + import sys + # For python 2.x and 3.x compatibility: 3.x has not raw_input builtin + # otherwise 2.x's input builtin function is too "smart" + if sys.version_info.major < 3: + input_function = raw_input + else: + input_function = input + import time + tstart=time.clock() + #pdb.set_trace() + unsorted=list(range(8000,1,-1)) + #print(sort(unsorted)) + print(shell_sort(unsorted)) + tend=time.clock() + print("read: %f s" % (tend - tstart)) + + #user_input = input_function('Enter numbers separated by a comma:\n') + #unsorted = [int(item) for item in user_input.split(',')] + #print(shell_sort(unsorted))