Skip to content
Related Articles
Open in App
Not now

Related Articles

Python Program for Comb Sort

Improve Article
Save Article
  • Last Updated : 03 Aug, 2022
Improve Article
Save Article

Comb Sort is mainly an improvement over Bubble Sort. Bubble sort always compares adjacent values. So all inversions are removed one by one. Comb Sort improves on Bubble Sort by using a gap of the size of more than 1. The gap starts with a large value and shrinks by a factor of 1.3 in every iteration until it reaches the value 1. Thus Comb Sort removes more than one inversion counts with one swap and performs better than Bubble Sort.

The shrink factor has been empirically found to be 1.3 (by testing Combsort on over 200,000 random lists) [Source: Wiki]
Although it works better than Bubble Sort on average, the worst case remains O(n2)



# Python program for implementation of CombSort
# To find next gap from current
def getNextGap(gap):
    # Shrink gap by Shrink factor
    gap = (gap * 10)/13
    if gap & lt
        return 1
    return gap
# Function to sort arr[] using Comb Sort
def combSort(arr):
    n = len(arr)
    # Initialize gap
    gap = n
    # Initialize swapped as true to make sure that
    # loop runs
    swapped = True
    # Keep running while gap is more than 1 and last
    # iteration caused a swap
    while gap != 1 or swapped == 1:
        # Find next gap
        gap = getNextGap(gap)
        # Initialize swapped as false so that we can
        # check if swap happened or not
        swapped = False
        # Compare all elements with current gap
        for i in range(0, n-gap):
            if arr[i] & gt
            arr[i + gap]:
                arr[i], arr[i + gap] = arr[i + gap], arr[i]
                swapped = True
# Driver code to test above
arr = [8, 4, 1, 3, -44, 23, -6, 28, 0]
print ( & quot
       Sorted array: & quot
for i in range(len(arr)):
# This code is contributed by Mohit Kumra

Output : 

Sorted array: 
-44 -6 0 1 3 4 8 23 28 56 

Time Complexity: The worst-case complexity of this algorithm is O(n2).

Auxiliary Space: O(1).

Please refer complete article on Comb Sort for more details!

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!