diff --git a/algorithms/Python/README.md b/algorithms/Python/README.md index 85c6d435..9d534396 100644 --- a/algorithms/Python/README.md +++ b/algorithms/Python/README.md @@ -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) diff --git a/algorithms/Python/sorting/comb_sort.py b/algorithms/Python/sorting/comb_sort.py new file mode 100644 index 00000000..ec563396 --- /dev/null +++ b/algorithms/Python/sorting/comb_sort.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)