
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program for the Comb Sort?
Comb sort is similar to bubble sort and cocktail sort. Comb sort doesn’t start off looking at adjacent elements but instead looks at elements a certain number of indexes apart, this is called the gap. The gap is defined as [n/c] where n is the number of elements and c is the shrink factor. After each iteration, this number is against divided by c and floored until eventually, the algorithm is looking at adjacent elements.
Input:53421 Output:12345
Explanation
Comb sort compare two elements with a gap which is defined as [n/c] where n is the number of elements and c is the shrink factor i.e. 1.3. After each iteration, this number is against divided by c and floored until eventually, the algorithm is looking at adjacent elements.
Example
#include <iostream> using namespace std; void combsort(int a[], int n) { int i, j, gap, swapped = 1; double temp; gap = n; while (gap > 1 || swapped == 1) { gap = gap * 10 / 1.3; if (gap == 9 || gap == 10) { gap = 11; } if (gap < 1) { gap = 1; } swapped = 0; for (i = 0, j = gap; j < n; i++, j++) { if (a[i] > a[j]) { temp = a[i]; a[i] = a[j]; a[j] = temp; swapped = 1; } } } } int main () { int n, i; int arr[] = { 5, 3, 4, 2, 1 }; n=5; combsort(arr, n); for(i = 0;i < n;i++) { cout<<arr[i]<<"\t"; } return 0; }
- Related Articles
- C++ Program for Comb Sort?
- Java Program for Comb Sort
- Comb Sort
- C/C++ Program for the Odd-Even Sort (Brick Sort)?
- C++ Program for the Cocktail Sort?
- C++ Program for the Gnome Sort?
- C/C++ Program for Odd-Even Sort (Brick Sort)?
- C++ Program for Cocktail Sort?
- C++ Program for Gnome Sort?
- C++ Program for Pigeonhole Sort?
- C Program for Selection Sort?
- C++ Program for Cycle Sort?
- C Program for Radix Sort
- C++ Program for the Recursive Bubble Sort?
- C++ Program for Recursive Bubble Sort?

Advertisements