diff --git a/CPP/combSort.cpp b/CPP/combSort.cpp new file mode 100644 index 0000000..e6af30d --- /dev/null +++ b/CPP/combSort.cpp @@ -0,0 +1,66 @@ +// C++ implementation of Comb Sort +#include +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 a[i+gap]) + { + swap(a[i], a[i+gap]); + swapped = true; + } + } + } +} + +// Driver program +int main() +{ + int n; //number of elements in array + cout<<"Nubers of elements: "; + cin>>n; + int arr[n]; + cout<<"Enter all elements seprated by space:"; + for(int i=0;i>arr[i],i++); + + combSort(arr, n); + + printf("Sorted array: \n"); + for (int i=0; i