mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-01-19 00:37:02 +00:00
Add comb sort algorithm
This commit is contained in:
parent
3a77380ed3
commit
5fb6b31ea9
58
sorts/comb_sort.py
Normal file
58
sorts/comb_sort.py
Normal file
|
@ -0,0 +1,58 @@
|
||||||
|
"""
|
||||||
|
Comb sort is a relatively simple sorting algorithm originally designed by Wlodzimierz Dobosiewicz in 1980.
|
||||||
|
Later it was rediscovered by Stephen Lacey and Richard Box in 1991. Comb sort improves on bubble sort.
|
||||||
|
|
||||||
|
This is pure python implementation of counting sort algorithm
|
||||||
|
For doctests run following command:
|
||||||
|
python -m doctest -v comb_sort.py
|
||||||
|
or
|
||||||
|
python3 -m doctest -v comb_sort.py
|
||||||
|
|
||||||
|
For manual testing run:
|
||||||
|
python comb_sort.py
|
||||||
|
"""
|
||||||
|
|
||||||
|
def comb_sort(data):
|
||||||
|
"""Pure implementation of comb sort algorithm in Python
|
||||||
|
:param collection: some mutable ordered collection with heterogeneous
|
||||||
|
comparable items inside
|
||||||
|
:return: the same collection ordered by ascending
|
||||||
|
Examples:
|
||||||
|
>>> comb_sort([0, 5, 3, 2, 2])
|
||||||
|
[0, 2, 2, 3, 5]
|
||||||
|
>>> comb_sort([])
|
||||||
|
[]
|
||||||
|
>>> comb_sort([-2, -5, -45])
|
||||||
|
[-45, -5, -2]
|
||||||
|
"""
|
||||||
|
shrink_factor = 1.3
|
||||||
|
gap = len(data)
|
||||||
|
swapped = True
|
||||||
|
i = 0
|
||||||
|
|
||||||
|
while gap > 1 or swapped:
|
||||||
|
# Update the gap value for a next comb
|
||||||
|
gap = int(float(gap) / shrink_factor)
|
||||||
|
|
||||||
|
swapped = False
|
||||||
|
i = 0
|
||||||
|
|
||||||
|
while gap + i < len(data):
|
||||||
|
if data[i] > data[i+gap]:
|
||||||
|
# Swap values
|
||||||
|
data[i], data[i+gap] = data[i+gap], data[i]
|
||||||
|
swapped = True
|
||||||
|
i += 1
|
||||||
|
|
||||||
|
return data
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
try:
|
||||||
|
raw_input # Python 2
|
||||||
|
except NameError:
|
||||||
|
raw_input = input # Python 3
|
||||||
|
|
||||||
|
user_input = raw_input('Enter numbers separated by a comma:\n').strip()
|
||||||
|
unsorted = [int(item) for item in user_input.split(',')]
|
||||||
|
print(comb_sort(unsorted))
|
Loading…
Reference in New Issue
Block a user