From 8a4b33534efb9bf35f707dca30fac0d73609d597 Mon Sep 17 00:00:00 2001 From: Jacob Bonek Date: Mon, 15 Aug 2016 23:18:42 -0400 Subject: [PATCH 1/5] Added Shell sort to readme --- README.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/README.md b/README.md index 3ea720683023..e0dc153305bd 100644 --- a/README.md +++ b/README.md @@ -73,6 +73,17 @@ __Properties__ ###### View the algorithm in [action][selection-toptal] +## Shell sort +![alt text][shell-image] + +From [Wikipedia][shell-wiki]: Shellsort is a generalization of insertion sort that allows the exchange of items that are far apart. The idea is to arrange the list of elements so that, starting anywherem considereing every nth element gives a sorted list. Such a list is said to be h-sorted. Equivanelty, it can be thought of as h intterleaved lists, each individually sorted. + +__Properties__ +* Worst case performance O(nlog2 2n) +* Best case performance O(n log n) +* Average case performance depends on gap sequence + +###### View the algorithm in [action][shell-toptal] ## Search Algorithms @@ -123,6 +134,11 @@ Mathematically a bijective function is used on the characters' positions to encr [selection-toptal]: https://www.toptal.com/developers/sorting-algorithms/selection-sort [selection-wiki]: https://en.wikipedia.org/wiki/Selection_sort [selection-image]: https://upload.wikimedia.org/wikipedia/commons/thumb/b/b0/Selection_sort_animation.gif/250px-Selection_sort_animation.gif "Selection Sort Sort" + +[shell-toptal]: https://www.toptal.com/developers/sorting-algorithms/shell-sort +[shell-wiki]: https://en.wikipedia.org/wiki/Shellsort +[shell-image]: https://upload.wikimedia.org/wikipedia/commons/d/d8/Sorting_shellsort_anim.gif "Shell Sort" + [caesar]: https://upload.wikimedia.org/wikipedia/commons/4/4a/Caesar_cipher_left_shift_of_3.svg [linear-wiki]: https://en.wikipedia.org/wiki/Linear_search [linear-image]: http://www.tutorialspoint.com/data_structures_algorithms/images/linear_search.gif From e359b6efd411aa21ed63e588386abe5fc38ff666 Mon Sep 17 00:00:00 2001 From: JakeBonek Date: Tue, 16 Aug 2016 12:43:02 -0400 Subject: [PATCH 2/5] Fixed typos and added the shell sort algorithm --- sorts/bogosort.py | 4 +-- sorts/bubble_sort.py | 2 +- sorts/heap_sort.py | 27 ++++++++++++++++++- sorts/insertion_sort.py | 6 ++--- sorts/merge_sort.py | 6 ++--- sorts/quick_sort.py | 4 +-- sorts/selection_sort.py | 15 ++++++----- sorts/shell_sort.py | 58 +++++++++++++++++++++++++++++++++++++++++ 8 files changed, 103 insertions(+), 19 deletions(-) create mode 100644 sorts/shell_sort.py diff --git a/sorts/bogosort.py b/sorts/bogosort.py index 7e9d07f504be..5abdab87d80d 100644 --- a/sorts/bogosort.py +++ b/sorts/bogosort.py @@ -1,5 +1,5 @@ """ -This is pure python implementation of bogosort algorithm +This is pure python implementation of the bogosort algorithm For doctests run following command: python -m doctest -v bogosort.py or @@ -47,6 +47,6 @@ def isSorted(collection): else: input_function = input - user_input = input_function('Enter numbers separated by coma:\n') + user_input = input_function('Enter numbers separated by a comma:\n') unsorted = [int(item) for item in user_input.split(',')] print(bogosort(unsorted)) diff --git a/sorts/bubble_sort.py b/sorts/bubble_sort.py index 2baaff9ae14e..3bacb306f254 100644 --- a/sorts/bubble_sort.py +++ b/sorts/bubble_sort.py @@ -47,6 +47,6 @@ def bubble_sort(collection): else: input_function = input - user_input = input_function('Enter numbers separated by coma:\n') + user_input = input_function('Enter numbers separated by a comma:\n') unsorted = [int(item) for item in user_input.split(',')] print(bubble_sort(unsorted)) diff --git a/sorts/heap_sort.py b/sorts/heap_sort.py index 5d1cbbbd0713..4c6026b8bf72 100644 --- a/sorts/heap_sort.py +++ b/sorts/heap_sort.py @@ -1,4 +1,14 @@ +""" +This is a pure python implementation of the heap sort algorithm. +For doctests run following command: +python -m doctest -v insertion_sort.py +or +python3 -m doctest -v insertion_sort.py + +For manual testing run: +python insertion_sort.py +""" from __future__ import print_function @@ -17,6 +27,21 @@ def heapify(unsorted,index,heap_size): heapify(unsorted,largest,heap_size) def heap_sort(unsorted): + """Pure implementation of the heap sort algorithm in Python + :param collection: some mutable ordered collection with heterogeneous + comparable items inside + :return: the same collection ordered by ascending + + Examples: + >>> heap_sort([0, 5, 3, 2, 2]) + [0, 2, 2, 3, 5] + + >>> heap_sort([]) + [] + + >>> heap_sort([-2, -5, -45]) + [-45, -5, -2] + """ n=len(unsorted) for i in range (n/2 - 1 , -1, -1) : heapify(unsorted,i,n) @@ -33,7 +58,7 @@ def heap_sort(unsorted): else: input_function = input - user_input = input_function('Enter numbers separated by coma:\n') + 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/insertion_sort.py b/sorts/insertion_sort.py index 501655e92eed..646ed68ca672 100644 --- a/sorts/insertion_sort.py +++ b/sorts/insertion_sort.py @@ -1,5 +1,5 @@ """ -This is pure python implementation of insertion sort algorithm +This is a pure python implementation of the insertion sort algorithm For doctests run following command: python -m doctest -v insertion_sort.py @@ -13,7 +13,7 @@ def insertion_sort(collection): - """Pure implementation of insertion sort algorithm in Python + """Pure implementation of the insertion sort algorithm in Python :param collection: some mutable ordered collection with heterogeneous comparable items inside @@ -51,6 +51,6 @@ def insertion_sort(collection): else: input_function = input - user_input = input_function('Enter numbers separated by coma:\n') + 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 c34c395af286..92a6780165ac 100644 --- a/sorts/merge_sort.py +++ b/sorts/merge_sort.py @@ -1,5 +1,5 @@ """ -This is pure python implementation of merge sort algorithm +This is a pure python implementation of the merge sort algorithm For doctests run following command: python -m doctest -v merge_sort.py @@ -13,7 +13,7 @@ def merge_sort(collection): - """Pure implementation of merge sort algorithm in Python + """Pure implementation of the merge sort algorithm in Python :param collection: some mutable ordered collection with heterogeneous comparable items inside @@ -71,6 +71,6 @@ def merge_sort(collection): else: input_function = input - user_input = input_function('Enter numbers separated by coma:\n') + user_input = input_function('Enter numbers separated by a comma:\n') unsorted = [int(item) for item in user_input.split(',')] print(merge_sort(unsorted)) diff --git a/sorts/quick_sort.py b/sorts/quick_sort.py index 4e6516cd81d2..f8e61ede4b52 100644 --- a/sorts/quick_sort.py +++ b/sorts/quick_sort.py @@ -1,5 +1,5 @@ """ -This is pure python implementation of quick sort algorithm +This is a pure python implementation of the quick sort algorithm For doctests run following command: python -m doctest -v quick_sort.py @@ -62,6 +62,6 @@ def quick_sort(collection): else: input_function = input - user_input = input_function('Enter numbers separated by coma:\n') + user_input = input_function('Enter numbers separated by a comma:\n') unsorted = [int(item) for item in user_input.split(',')] print(sort(unsorted)) diff --git a/sorts/selection_sort.py b/sorts/selection_sort.py index fbb94226d807..80c3062265b1 100644 --- a/sorts/selection_sort.py +++ b/sorts/selection_sort.py @@ -1,5 +1,5 @@ """ -This is pure python implementation of selection sort algorithm +This is a pure python implementation of the selection sort algorithm For doctests run following command: python -m doctest -v selection_sort.py @@ -13,7 +13,11 @@ def selection_sort(collection): - """Pure implementation of selection sort algorithm in Python + """Pure implementation of the selection sort algorithm in Python + :param collection: some mutable ordered collection with heterogeneous + comparable items inside + :return: the same collection ordered by ascending + Examples: >>> selection_sort([0, 5, 3, 2, 2]) @@ -24,11 +28,8 @@ def selection_sort(collection): >>> selection_sort([-2, -5, -45]) [-45, -5, -2] - - :param collection: some mutable ordered collection with heterogeneous - comparable items inside - :return: the same collection ordered by ascending """ + length = len(collection) for i in range(length): least = i @@ -50,6 +51,6 @@ def selection_sort(collection): else: input_function = input - user_input = input_function('Enter numbers separated by coma:\n') + user_input = input_function('Enter numbers separated by a comma:\n') unsorted = [int(item) for item in user_input.split(',')] print(selection_sort(unsorted)) diff --git a/sorts/shell_sort.py b/sorts/shell_sort.py new file mode 100644 index 000000000000..4dc37240b9db --- /dev/null +++ b/sorts/shell_sort.py @@ -0,0 +1,58 @@ +""" +This is a pure python implementation of the shell sort algorithm + +For doctests run following command: +python -m doctest -v shell_sort.py +or +python3 -m doctest -v shell_sort.py + +For manual testing run: +python shell_sort.py +""" +from __future__ import print_function + + +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 + +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)) From de8c5cbf006863cf386741cc19b250bd048f96bb Mon Sep 17 00:00:00 2001 From: JakeBonek Date: Tue, 16 Aug 2016 12:56:38 -0400 Subject: [PATCH 3/5] Fixed typos --- sorts/bogosort.py | 4 ++-- sorts/heap_sort.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/sorts/bogosort.py b/sorts/bogosort.py index 5abdab87d80d..b8cb5b31b20e 100644 --- a/sorts/bogosort.py +++ b/sorts/bogosort.py @@ -1,5 +1,5 @@ """ -This is pure python implementation of the bogosort algorithm +This is a pure python implementation of the bogosort algorithm For doctests run following command: python -m doctest -v bogosort.py or @@ -12,7 +12,7 @@ import random def bogosort(collection): - """Pure implementation of bogosort algorithm in Python + """Pure implementation of the bogosort algorithm in Python :param collection: some mutable ordered collection with heterogeneous comparable items inside :return: the same collection ordered by ascending diff --git a/sorts/heap_sort.py b/sorts/heap_sort.py index 4c6026b8bf72..6d731cd4b162 100644 --- a/sorts/heap_sort.py +++ b/sorts/heap_sort.py @@ -2,9 +2,9 @@ This is a pure python implementation of the heap sort algorithm. For doctests run following command: -python -m doctest -v insertion_sort.py +python -m doctest -v heap_sort.py or -python3 -m doctest -v insertion_sort.py +python3 -m doctest -v heap_sort.py For manual testing run: python insertion_sort.py From 9e031e9783c63f2cbd7525e2fa35245c1dd52a8d Mon Sep 17 00:00:00 2001 From: JakeBonek Date: Tue, 16 Aug 2016 15:56:06 -0400 Subject: [PATCH 4/5] Fixed spacing --- sorts/heap_sort.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/sorts/heap_sort.py b/sorts/heap_sort.py index 6d731cd4b162..7882c200f8b5 100644 --- a/sorts/heap_sort.py +++ b/sorts/heap_sort.py @@ -42,8 +42,8 @@ def heap_sort(unsorted): >>> heap_sort([-2, -5, -45]) [-45, -5, -2] """ - n=len(unsorted) - for i in range (n/2 - 1 , -1, -1) : + n=len(unsorted) + for i in range (n/2 - 1 , -1, -1): heapify(unsorted,i,n) for i in range(n - 1, -1, -1): unsorted[0], unsorted[i] = unsorted[i], unsorted[0] @@ -61,4 +61,3 @@ def heap_sort(unsorted): user_input = input_function('Enter numbers separated by a comma:\n') unsorted = [int(item) for item in user_input.split(',')] print (heap_sort(unsorted)) - From d31387455ec026939971a03b301cb3e5af2aaf7a Mon Sep 17 00:00:00 2001 From: JakeBonek Date: Tue, 16 Aug 2016 16:03:02 -0400 Subject: [PATCH 5/5] Fixed spacing --- sorts/heap_sort.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/sorts/heap_sort.py b/sorts/heap_sort.py index 7882c200f8b5..ec262ad6bae4 100644 --- a/sorts/heap_sort.py +++ b/sorts/heap_sort.py @@ -42,13 +42,13 @@ def heap_sort(unsorted): >>> heap_sort([-2, -5, -45]) [-45, -5, -2] """ - n=len(unsorted) - for i in range (n/2 - 1 , -1, -1): - heapify(unsorted,i,n) - for i in range(n - 1, -1, -1): - unsorted[0], unsorted[i] = unsorted[i], unsorted[0] - heapify(unsorted,0,i) - return unsorted + n=len(unsorted) + for i in range (n/2 - 1 , -1, -1): + heapify(unsorted,i,n) + for i in range(n - 1, -1, -1): + unsorted[0], unsorted[i] = unsorted[i], unsorted[0] + heapify(unsorted,0,i) + return unsorted if __name__ == '__main__':