forked from silent-killer-11/Hacktoberfest-2022
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcomb.sort.cpp
More file actions
54 lines (46 loc) · 920 Bytes
/
comb.sort.cpp
File metadata and controls
54 lines (46 loc) · 920 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//Program for Comb Sort in C++
#include<bits/stdc++.h>
#include<time.h>
using namespace std;
int getNextGap(int gap) {
gap = (gap*10)/13;
if (gap < 1)
return 1;
return gap;
}
void Comb_Sort(int arr[], int n) {
int gap = n;
bool swapped = true;
while (gap != 1 || swapped == true) {
gap = getNextGap(gap);
swapped = false;
for (int i=0; i<n-gap; i++) {
if (arr[i] > arr[i+gap]) {
swap(arr[i], arr[i+gap]);
swapped = true;
}
}
}
}
void print_array(int arr[], int n) {
for(int i=0;i<n;i++) {
cout<<arr[i]<<" ";
}
cout<<endl;
}
int main(void) {
int n;
srand(time(0));
//cout<<"Enter the size of n:"<<endl;
cin>>n;
int arr[n+1];
for(int i=0;i<n;i++) {
arr[i]=rand()%1000;
}
cout<<"Before sort -- "<<endl;
print_array(arr,n);
Comb_Sort(arr, n);
cout<<"After sort -- "<<endl;
print_array(arr,n);
return 0;
}