mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-07-05 09:21:13 +08:00
Fixed typos and added the shell sort algorithm
This commit is contained in:
@ -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 @@ if __name__ == '__main__':
|
||||
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))
|
||||
|
||||
|
Reference in New Issue
Block a user