Comb Sort
The Comb Sort is a variant of the Bubble Sort. The Comb Sort increases the gap used in comparisons and exchanges. In bubble sort, when any two elements are compared, they always have a gap (distance from each other) of 1. The basic idea of comb sort is that the gap can be much more than 1.
## # Python's algorithm of Combsort sort def combsort(num): gap = len(num) swaps = True while gap > 1 or swaps: gap = max(1, int(gap / 1.25)) # minimum gap is 1 swaps = False for i in range(len(num) - gap): j = i+gap if num[i] > num[j]: num[i], num[j] = num[j], num[i] swaps = True num_list = [75, 16, 55, 19, 48, 14, 2, 61, 22, 100] print("Before: ", num_list) combsort(num_list) print("After: ", num_list)
Sample output of above program.
C:\programs\algorithms>pep8 --first example.py
C:\programs\algorithms>python example.py
Before: [75, 16, 55, 19, 48, 14, 2, 61, 22, 100]
After: [2, 14, 16, 19, 22, 48, 55, 61, 75, 100]
C:\programs\algorithms>
C:\programs\algorithms>python example.py
Before: [75, 16, 55, 19, 48, 14, 2, 61, 22, 100]
After: [2, 14, 16, 19, 22, 48, 55, 61, 75, 100]
C:\programs\algorithms>
2018-04-05T14:00:00+05:30
2018-04-05T14:00:00+05:30
Amit Arora
Amit Arora
Python Programming Tutorial
Python
Practical Solution