Skip to content

Commit

Permalink
Fixed typos and added the shell sort algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeBonek committed Aug 16, 2016
1 parent 8a4b335 commit e359b6e
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 19 deletions.
4 changes: 2 additions & 2 deletions sorts/bogosort.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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))
2 changes: 1 addition & 1 deletion sorts/bubble_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
27 changes: 26 additions & 1 deletion sorts/heap_sort.py
Original file line number Diff line number Diff line change
@@ -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


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

6 changes: 3 additions & 3 deletions sorts/insertion_sort.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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))
6 changes: 3 additions & 3 deletions sorts/merge_sort.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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))
4 changes: 2 additions & 2 deletions sorts/quick_sort.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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))
15 changes: 8 additions & 7 deletions sorts/selection_sort.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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])
Expand All @@ -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
Expand All @@ -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))
58 changes: 58 additions & 0 deletions sorts/shell_sort.py
Original file line number Diff line number Diff line change
@@ -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))

0 comments on commit e359b6e

Please sign in to comment.