chore(Python): add comb_sort (#244)

pull/264/head
Md Mahedi Hasan Riday 2021-04-25 21:09:59 +06:00 committed by GitHub
parent 6cbc1f21c2
commit 25b71cb15a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 3 deletions

View File

@ -20,9 +20,10 @@
## Sorting
1. [Bubble Sort](sorting/bubble_sort.py)
2. [Insertion Sort](sorting/insertion_sort.py)
3. [Quicksort](sorting/quicksort.py)
4. [Selection Sort](sorting/selection_sort.py)
2. [Comb Sort](sorting/comb_sort.py)
3. [Insertion Sort](sorting/insertion_sort.py)
4. [Quicksort](sorting/quicksort.py)
5. [Selection Sort](sorting/selection_sort.py)
## Strings
1. [Is Good Str](strings/is_good_str.py)

View File

@ -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)