forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComb_Sort.c
61 lines (54 loc) · 1.08 KB
/
Comb_Sort.c
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
55
56
57
58
59
60
61
#include <stdio.h>
void combSort(int arr[], int n)
{
// initialize gap with array length
int gap = n;
int flag= 1;
while (gap > 1 || flag == 1)
{
// updating gap value by using shrink factor as 1.3
gap = (gap * 10) / 13;
// if Gap is less than 1, take gap as 1
if(gap<1)
gap= 1;
flag= 0;
// Compare all elements with the obtained gap value
for (int i = 0; i < (n - gap); i++)
{
// Swap arr[i] and arr[i+gap] if arr[i] is greater and update the flag
if (arr[i] > arr[i + gap])
{
int temp;
temp=arr[i];
arr[i]= arr[i+gap];
arr[i+gap]=temp;
flag= 1;
}
}
}
}
// Driver function
int main()
{
int n;
printf("Enter the number of elements ");
scanf("%d",&n);
int arr[n];
printf("Enter the elements ");
for (int i=0;i<n;i++)
scanf("%d",&arr[i]);
// Call to combSort function
combSort(arr, n);
// Printing sorted array
printf("Sorted array:\n");
for (int i = 0; i < n; i++)
printf("%d ",arr[i]);
}
// INPUT:Enter the number of elements 5
//Enter the elements -10
//9
//0
//8
//2
//OUTPUT:Sorted array:
//-10 0 2 8 9