chore(Python): add comb_sort (#244)
parent
6cbc1f21c2
commit
25b71cb15a
|
@ -20,9 +20,10 @@
|
||||||
|
|
||||||
## Sorting
|
## Sorting
|
||||||
1. [Bubble Sort](sorting/bubble_sort.py)
|
1. [Bubble Sort](sorting/bubble_sort.py)
|
||||||
2. [Insertion Sort](sorting/insertion_sort.py)
|
2. [Comb Sort](sorting/comb_sort.py)
|
||||||
3. [Quicksort](sorting/quicksort.py)
|
3. [Insertion Sort](sorting/insertion_sort.py)
|
||||||
4. [Selection Sort](sorting/selection_sort.py)
|
4. [Quicksort](sorting/quicksort.py)
|
||||||
|
5. [Selection Sort](sorting/selection_sort.py)
|
||||||
|
|
||||||
## Strings
|
## Strings
|
||||||
1. [Is Good Str](strings/is_good_str.py)
|
1. [Is Good Str](strings/is_good_str.py)
|
||||||
|
|
|
@ -0,0 +1,34 @@
|
||||||
|
'''
|
||||||
|
Comb sort is a comparison based sorting algorithm and is an improved from of bubble sort.
|
||||||
|
|
||||||
|
Best case time complexity Θ(n log n)
|
||||||
|
Worst case time complexity O(n^2)
|
||||||
|
'''
|
||||||
|
|
||||||
|
arr = [12, 3, 7, 22, -12, 100, 1]
|
||||||
|
|
||||||
|
|
||||||
|
def combSort(arr):
|
||||||
|
"""
|
||||||
|
>>> combSort(arr)
|
||||||
|
[-12, 1, 3, 7, 12, 22, 100]
|
||||||
|
"""
|
||||||
|
gap = len(arr)
|
||||||
|
shrink = int(gap * 10 / 13)
|
||||||
|
sorted = False
|
||||||
|
while gap > 1 or sorted == False:
|
||||||
|
gap = int(gap / shrink)
|
||||||
|
if gap <= 1:
|
||||||
|
gap = 1
|
||||||
|
sorted = True
|
||||||
|
for i in range(len(arr)-gap):
|
||||||
|
if arr[i] > arr[i+gap]:
|
||||||
|
arr[i], arr[i+gap] = arr[i+gap], arr[i]
|
||||||
|
sorted = False
|
||||||
|
return arr
|
||||||
|
|
||||||
|
|
||||||
|
combSort(arr)
|
||||||
|
print("Sorted array is : ")
|
||||||
|
for ele in arr:
|
||||||
|
print(ele)
|
Loading…
Reference in New Issue