C++ Program for Comb Sort Last Updated : 28 Jul, 2022 Comments Improve Suggest changes Like Article Like Report 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 gap of size 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, worst case remains O(n2). CPP // C++ implementation of Comb Sort #include <bits/stdc++.h> using namespace std; // To find gap between elements int getNextGap(int gap) { // Shrink gap by Shrink factor gap = (gap * 10) / 13; if (gap < 1) return 1; return gap; } // Function to sort a[0..n-1] using Comb Sort void combSort(int a[], int n) { // Initialize gap int gap = n; // Initialize swapped as true to make sure that // loop runs bool swapped = true; // Keep running while gap is more than 1 and last // iteration caused a swap while (gap != 1 || swapped == true) { // 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 (int i = 0; i < n - gap; i++) { if (a[i] > a[i + gap]) { swap(a[i], a[i + gap]); swapped = true; } } } } // Driver program int main() { int a[] = { 8, 4, 1, 56, 3, -44, 23, -6, 28, 0 }; int n = sizeof(a) / sizeof(a[0]); combSort(a, n); printf("Sorted array: \n"); for (int i = 0; i < n; i++) printf("%d ", a[i]); return 0; } 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! Comment More infoAdvertise with us Next Article C++ Program for Comb Sort kartik Follow Improve Article Tags : Sorting C++ Programs DSA Practice Tags : Sorting Similar Reads C++ Program for Cocktail Sort Cocktail Sort is a variation of Bubble sort. The Bubble sort algorithm always traverses elements from left and moves the largest element to its correct position in first iteration and second largest in second iteration and so on. Cocktail Sort traverses through a given array in both directions alter 3 min read C++ Program for Bitonic Sort Bitonic Sequence A sequence is called Bitonic if it is first increasing, then decreasing. In other words, an array arr[0..n-i] is Bitonic if there exists an index i where 0<=i<=n-1 such that x0 <= x1 â¦..<= xi and xi >= xi+1â¦.. >= xn-1 A sequence, sorted in increasing order is consi 4 min read C++ Program for ShellSort In shellSort, we make the array h-sorted for a large value of h. We keep reducing the value of h until it becomes 1. An array is said to be h-sorted if all sublists of every h'th element is sorted. CPP // C++ implementation of Shell Sort #include <iostream> /* function to sort arr using shellS 2 min read C++ Program for Heap Sort Heap sort is a comparison-based sorting technique based on the Binary Heap data structure. It is similar to the selection sort where we first find the maximum element and place the maximum element at the end. We repeat the same process for the remaining element. Recommended PracticeHeap SortTry It! 3 min read C++ Program for Quick Sort Quick Sort is one of the most efficient sorting algorithms available to sort the given dataset. It is known for its efficiency in handling large datasets which made it a go-to choice for programmers looking to optimize their code.In C++, the STL's sort() function uses a mix of different sorting algo 4 min read C++ Program For Merge Sort Merge Sort is a comparison-based sorting algorithm that uses divide and conquer paradigm to sort the given dataset. It divides the dataset into two halves, calls itself for these two halves, and then it merges the two sorted halves.In this article, we will learn how to implement merge sort in a C++ 4 min read C++ Program For Insertion Sort Insertion sort is a simple sorting algorithm that works by dividing the array into two parts, sorted and unsorted part. In each iteration, the first element from the unsorted subarray is taken and it is placed at its correct position in the sorted array. In this article, we will learn to write a C++ 3 min read C++ Program For Selection Sort The selection sort algorithm sorts an array by repeatedly finding the minimum element (considering ascending order) from unsorted part and putting it at the beginning. The algorithm maintains two subarrays in a given array. The subarray which is already sorted. Remaining subarray which is unsorted. 4 min read C++ Program for Recursive Bubble Sort Background: Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. Following is the iterative Bubble sort algorithm : // Iterative Bubble Sort bubbleSort(arr[], n) { for (i = 0; i n-1; i++) // Last i elements are already 2 min read C/C++ Program for Odd-Even Sort / Brick Sort This is basically a variation of bubble-sort. This algorithm is divided into two phases- Odd and Even Phase. The algorithm runs until the array elements are sorted and in each iteration two phases occurs- Odd and Even Phases. In the odd phase, we perform a bubble sort on odd indexed elements and in 2 min read Like