Merge pull request #19 from JakeBonek/master

Added Shell Sort
This commit is contained in:
Harshil 2016-08-17 17:11:28 +05:30 committed by GitHub
commit f18d6dd92a
9 changed files with 127 additions and 28 deletions

View File

@ -73,6 +73,17 @@ __Properties__
###### View the algorithm in [action][selection-toptal] ###### 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 ## 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-toptal]: https://www.toptal.com/developers/sorting-algorithms/selection-sort
[selection-wiki]: https://en.wikipedia.org/wiki/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" [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 [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-wiki]: https://en.wikipedia.org/wiki/Linear_search
[linear-image]: http://www.tutorialspoint.com/data_structures_algorithms/images/linear_search.gif [linear-image]: http://www.tutorialspoint.com/data_structures_algorithms/images/linear_search.gif

View File

@ -1,5 +1,5 @@
""" """
This is pure python implementation of bogosort algorithm This is a pure python implementation of the bogosort algorithm
For doctests run following command: For doctests run following command:
python -m doctest -v bogosort.py python -m doctest -v bogosort.py
or or
@ -12,7 +12,7 @@ from __future__ import print_function
import random import random
def bogosort(collection): 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 :param collection: some mutable ordered collection with heterogeneous
comparable items inside comparable items inside
:return: the same collection ordered by ascending :return: the same collection ordered by ascending
@ -47,6 +47,6 @@ if __name__ == '__main__':
else: else:
input_function = input 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(',')] unsorted = [int(item) for item in user_input.split(',')]
print(bogosort(unsorted)) print(bogosort(unsorted))

View File

@ -47,6 +47,6 @@ if __name__ == '__main__':
else: else:
input_function = input 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(',')] unsorted = [int(item) for item in user_input.split(',')]
print(bubble_sort(unsorted)) print(bubble_sort(unsorted))

View File

@ -1,4 +1,14 @@
"""
This is a pure python implementation of the heap sort algorithm.
For doctests run following command:
python -m doctest -v heap_sort.py
or
python3 -m doctest -v heap_sort.py
For manual testing run:
python insertion_sort.py
"""
from __future__ import print_function from __future__ import print_function
@ -17,13 +27,28 @@ def heapify(unsorted,index,heap_size):
heapify(unsorted,largest,heap_size) heapify(unsorted,largest,heap_size)
def heap_sort(unsorted): def heap_sort(unsorted):
n=len(unsorted) """Pure implementation of the heap sort algorithm in Python
for i in range (n/2 - 1 , -1, -1) : :param collection: some mutable ordered collection with heterogeneous
heapify(unsorted,i,n) comparable items inside
for i in range(n - 1, -1, -1): :return: the same collection ordered by ascending
unsorted[0], unsorted[i] = unsorted[i], unsorted[0]
heapify(unsorted,0,i) Examples:
return unsorted >>> 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)
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__': if __name__ == '__main__':
@ -33,7 +58,6 @@ if __name__ == '__main__':
else: else:
input_function = input 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(',')] unsorted = [int(item) for item in user_input.split(',')]
print (heap_sort(unsorted)) print (heap_sort(unsorted))

View File

@ -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: For doctests run following command:
python -m doctest -v insertion_sort.py python -m doctest -v insertion_sort.py
@ -13,7 +13,7 @@ from __future__ import print_function
def insertion_sort(collection): 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 :param collection: some mutable ordered collection with heterogeneous
comparable items inside comparable items inside
@ -51,6 +51,6 @@ if __name__ == '__main__':
else: else:
input_function = input 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(',')] unsorted = [int(item) for item in user_input.split(',')]
print(insertion_sort(unsorted)) print(insertion_sort(unsorted))

View File

@ -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: For doctests run following command:
python -m doctest -v merge_sort.py python -m doctest -v merge_sort.py
@ -13,7 +13,7 @@ from __future__ import print_function
def merge_sort(collection): 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 :param collection: some mutable ordered collection with heterogeneous
comparable items inside comparable items inside
@ -71,6 +71,6 @@ if __name__ == '__main__':
else: else:
input_function = input 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(',')] unsorted = [int(item) for item in user_input.split(',')]
print(merge_sort(unsorted)) print(merge_sort(unsorted))

View File

@ -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: For doctests run following command:
python -m doctest -v quick_sort.py python -m doctest -v quick_sort.py
@ -62,6 +62,6 @@ if __name__ == '__main__':
else: else:
input_function = input 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(',')] unsorted = [int(item) for item in user_input.split(',')]
print(sort(unsorted)) print(sort(unsorted))

View File

@ -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: For doctests run following command:
python -m doctest -v selection_sort.py python -m doctest -v selection_sort.py
@ -13,7 +13,11 @@ from __future__ import print_function
def selection_sort(collection): 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: Examples:
>>> selection_sort([0, 5, 3, 2, 2]) >>> selection_sort([0, 5, 3, 2, 2])
@ -24,11 +28,8 @@ def selection_sort(collection):
>>> selection_sort([-2, -5, -45]) >>> selection_sort([-2, -5, -45])
[-45, -5, -2] [-45, -5, -2]
:param collection: some mutable ordered collection with heterogeneous
comparable items inside
:return: the same collection ordered by ascending
""" """
length = len(collection) length = len(collection)
for i in range(length): for i in range(length):
least = i least = i
@ -50,6 +51,6 @@ if __name__ == '__main__':
else: else:
input_function = input 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(',')] unsorted = [int(item) for item in user_input.split(',')]
print(selection_sort(unsorted)) print(selection_sort(unsorted))

58
sorts/shell_sort.py Normal file
View File

@ -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))