Python/sorts/comb_sort.py

55 lines
1.5 KiB
Python
Raw Normal View History

2018-10-02 02:43:25 +00:00
"""
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.
2018-10-02 02:46:47 +00:00
This is pure python implementation of comb sort algorithm
2018-10-02 02:43:25 +00:00
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
"""
2019-10-05 05:14:13 +00:00
2018-10-02 02:43:25 +00:00
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:
2018-10-02 02:46:47 +00:00
# Update the gap value for a next comb
2018-10-02 02:43:25 +00:00
gap = int(float(gap) / shrink_factor)
swapped = False
i = 0
while gap + i < len(data):
2019-10-05 05:14:13 +00:00
if data[i] > data[i + gap]:
2018-10-02 02:46:47 +00:00
# Swap values
2019-10-05 05:14:13 +00:00
data[i], data[i + gap] = data[i + gap], data[i]
2018-10-02 02:43:25 +00:00
swapped = True
i += 1
return data
2019-10-05 05:14:13 +00:00
if __name__ == "__main__":
user_input = input("Enter numbers separated by a comma:\n").strip()
unsorted = [int(item) for item in user_input.split(",")]
2018-10-02 02:43:25 +00:00
print(comb_sort(unsorted))