Skip to content

Commit f18d6dd

Browse files
authored
Merge pull request TheAlgorithms#19 from JakeBonek/master
Added Shell Sort
2 parents 95ff389 + d313874 commit f18d6dd

9 files changed

+127
-28
lines changed

README.md

+16
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,17 @@ __Properties__
7373

7474
###### View the algorithm in [action][selection-toptal]
7575

76+
## Shell sort
77+
![alt text][shell-image]
78+
79+
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.
80+
81+
__Properties__
82+
* Worst case performance O(nlog2 2n)
83+
* Best case performance O(n log n)
84+
* Average case performance depends on gap sequence
85+
86+
###### View the algorithm in [action][shell-toptal]
7687

7788
## Search Algorithms
7889

@@ -123,6 +134,11 @@ Mathematically a bijective function is used on the characters' positions to encr
123134
[selection-toptal]: https://www.toptal.com/developers/sorting-algorithms/selection-sort
124135
[selection-wiki]: https://en.wikipedia.org/wiki/Selection_sort
125136
[selection-image]: https://upload.wikimedia.org/wikipedia/commons/thumb/b/b0/Selection_sort_animation.gif/250px-Selection_sort_animation.gif "Selection Sort Sort"
137+
138+
[shell-toptal]: https://www.toptal.com/developers/sorting-algorithms/shell-sort
139+
[shell-wiki]: https://en.wikipedia.org/wiki/Shellsort
140+
[shell-image]: https://upload.wikimedia.org/wikipedia/commons/d/d8/Sorting_shellsort_anim.gif "Shell Sort"
141+
126142
[caesar]: https://upload.wikimedia.org/wikipedia/commons/4/4a/Caesar_cipher_left_shift_of_3.svg
127143
[linear-wiki]: https://en.wikipedia.org/wiki/Linear_search
128144
[linear-image]: http://www.tutorialspoint.com/data_structures_algorithms/images/linear_search.gif

sorts/bogosort.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
This is pure python implementation of bogosort algorithm
2+
This is a pure python implementation of the bogosort algorithm
33
For doctests run following command:
44
python -m doctest -v bogosort.py
55
or
@@ -12,7 +12,7 @@
1212
import random
1313

1414
def bogosort(collection):
15-
"""Pure implementation of bogosort algorithm in Python
15+
"""Pure implementation of the bogosort algorithm in Python
1616
:param collection: some mutable ordered collection with heterogeneous
1717
comparable items inside
1818
:return: the same collection ordered by ascending
@@ -47,6 +47,6 @@ def isSorted(collection):
4747
else:
4848
input_function = input
4949

50-
user_input = input_function('Enter numbers separated by coma:\n')
50+
user_input = input_function('Enter numbers separated by a comma:\n')
5151
unsorted = [int(item) for item in user_input.split(',')]
5252
print(bogosort(unsorted))

sorts/bubble_sort.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,6 @@ def bubble_sort(collection):
4747
else:
4848
input_function = input
4949

50-
user_input = input_function('Enter numbers separated by coma:\n')
50+
user_input = input_function('Enter numbers separated by a comma:\n')
5151
unsorted = [int(item) for item in user_input.split(',')]
5252
print(bubble_sort(unsorted))

sorts/heap_sort.py

+33-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
1+
"""
2+
This is a pure python implementation of the heap sort algorithm.
13
4+
For doctests run following command:
5+
python -m doctest -v heap_sort.py
6+
or
7+
python3 -m doctest -v heap_sort.py
8+
9+
For manual testing run:
10+
python insertion_sort.py
11+
"""
212
from __future__ import print_function
313

414

@@ -17,13 +27,28 @@ def heapify(unsorted,index,heap_size):
1727
heapify(unsorted,largest,heap_size)
1828

1929
def heap_sort(unsorted):
20-
n=len(unsorted)
21-
for i in range (n/2 - 1 , -1, -1) :
22-
heapify(unsorted,i,n)
23-
for i in range(n - 1, -1, -1):
24-
unsorted[0], unsorted[i] = unsorted[i], unsorted[0]
25-
heapify(unsorted,0,i)
26-
return unsorted
30+
"""Pure implementation of the heap sort algorithm in Python
31+
:param collection: some mutable ordered collection with heterogeneous
32+
comparable items inside
33+
:return: the same collection ordered by ascending
34+
35+
Examples:
36+
>>> heap_sort([0, 5, 3, 2, 2])
37+
[0, 2, 2, 3, 5]
38+
39+
>>> heap_sort([])
40+
[]
41+
42+
>>> heap_sort([-2, -5, -45])
43+
[-45, -5, -2]
44+
"""
45+
n=len(unsorted)
46+
for i in range (n/2 - 1 , -1, -1):
47+
heapify(unsorted,i,n)
48+
for i in range(n - 1, -1, -1):
49+
unsorted[0], unsorted[i] = unsorted[i], unsorted[0]
50+
heapify(unsorted,0,i)
51+
return unsorted
2752

2853

2954
if __name__ == '__main__':
@@ -33,7 +58,6 @@ def heap_sort(unsorted):
3358
else:
3459
input_function = input
3560

36-
user_input = input_function('Enter numbers separated by coma:\n')
61+
user_input = input_function('Enter numbers separated by a comma:\n')
3762
unsorted = [int(item) for item in user_input.split(',')]
3863
print (heap_sort(unsorted))
39-

sorts/insertion_sort.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
This is pure python implementation of insertion sort algorithm
2+
This is a pure python implementation of the insertion sort algorithm
33
44
For doctests run following command:
55
python -m doctest -v insertion_sort.py
@@ -13,7 +13,7 @@
1313

1414

1515
def insertion_sort(collection):
16-
"""Pure implementation of insertion sort algorithm in Python
16+
"""Pure implementation of the insertion sort algorithm in Python
1717
1818
:param collection: some mutable ordered collection with heterogeneous
1919
comparable items inside
@@ -51,6 +51,6 @@ def insertion_sort(collection):
5151
else:
5252
input_function = input
5353

54-
user_input = input_function('Enter numbers separated by coma:\n')
54+
user_input = input_function('Enter numbers separated by a comma:\n')
5555
unsorted = [int(item) for item in user_input.split(',')]
5656
print(insertion_sort(unsorted))

sorts/merge_sort.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
This is pure python implementation of merge sort algorithm
2+
This is a pure python implementation of the merge sort algorithm
33
44
For doctests run following command:
55
python -m doctest -v merge_sort.py
@@ -13,7 +13,7 @@
1313

1414

1515
def merge_sort(collection):
16-
"""Pure implementation of merge sort algorithm in Python
16+
"""Pure implementation of the merge sort algorithm in Python
1717
1818
:param collection: some mutable ordered collection with heterogeneous
1919
comparable items inside
@@ -71,6 +71,6 @@ def merge_sort(collection):
7171
else:
7272
input_function = input
7373

74-
user_input = input_function('Enter numbers separated by coma:\n')
74+
user_input = input_function('Enter numbers separated by a comma:\n')
7575
unsorted = [int(item) for item in user_input.split(',')]
7676
print(merge_sort(unsorted))

sorts/quick_sort.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
This is pure python implementation of quick sort algorithm
2+
This is a pure python implementation of the quick sort algorithm
33
44
For doctests run following command:
55
python -m doctest -v quick_sort.py
@@ -62,6 +62,6 @@ def quick_sort(collection):
6262
else:
6363
input_function = input
6464

65-
user_input = input_function('Enter numbers separated by coma:\n')
65+
user_input = input_function('Enter numbers separated by a comma:\n')
6666
unsorted = [int(item) for item in user_input.split(',')]
6767
print(sort(unsorted))

sorts/selection_sort.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
This is pure python implementation of selection sort algorithm
2+
This is a pure python implementation of the selection sort algorithm
33
44
For doctests run following command:
55
python -m doctest -v selection_sort.py
@@ -13,7 +13,11 @@
1313

1414

1515
def selection_sort(collection):
16-
"""Pure implementation of selection sort algorithm in Python
16+
"""Pure implementation of the selection 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+
1721
1822
Examples:
1923
>>> selection_sort([0, 5, 3, 2, 2])
@@ -24,11 +28,8 @@ def selection_sort(collection):
2428
2529
>>> selection_sort([-2, -5, -45])
2630
[-45, -5, -2]
27-
28-
:param collection: some mutable ordered collection with heterogeneous
29-
comparable items inside
30-
:return: the same collection ordered by ascending
3131
"""
32+
3233
length = len(collection)
3334
for i in range(length):
3435
least = i
@@ -50,6 +51,6 @@ def selection_sort(collection):
5051
else:
5152
input_function = input
5253

53-
user_input = input_function('Enter numbers separated by coma:\n')
54+
user_input = input_function('Enter numbers separated by a comma:\n')
5455
unsorted = [int(item) for item in user_input.split(',')]
5556
print(selection_sort(unsorted))

sorts/shell_sort.py

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
"""
2+
This is a pure python implementation of the shell sort algorithm
3+
4+
For doctests run following command:
5+
python -m doctest -v shell_sort.py
6+
or
7+
python3 -m doctest -v shell_sort.py
8+
9+
For manual testing run:
10+
python shell_sort.py
11+
"""
12+
from __future__ import print_function
13+
14+
15+
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+
45+
return collection
46+
47+
if __name__ == '__main__':
48+
import sys
49+
# For python 2.x and 3.x compatibility: 3.x has not raw_input builtin
50+
# otherwise 2.x's input builtin function is too "smart"
51+
if sys.version_info.major < 3:
52+
input_function = raw_input
53+
else:
54+
input_function = input
55+
56+
user_input = input_function('Enter numbers separated by a comma:\n')
57+
unsorted = [int(item) for item in user_input.split(',')]
58+
print(shell_sort(unsorted))

0 commit comments

Comments
 (0)