-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComboSort.c
More file actions
51 lines (38 loc) · 958 Bytes
/
ComboSort.c
File metadata and controls
51 lines (38 loc) · 958 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
#include<stdio.h>
#include<stdlib.h>
// Program to perform comb sort on an integer with a gap factor of 1.3
// Time Complexity: Best case O(n), Worst case O(n^2)
// Performs better than bubble sort on average
void combsort(int* arr, int size) {
int gap = (int)(size / 1.3);
int sorted = 0;
while (!sorted) {
sorted = 1;
for (int i = 0; i + gap < size; i++) {
if (arr[i] > arr[i+gap])
{
int temp = arr[i];
arr[i] = arr[i+gap];
arr[i+gap] = temp;
sorted = 0;
}
}
gap = (int)(gap / 1.3);
}
}
int main()
{
int n;
printf("Enter the size of the array:\n");
scanf("%d", &n);
int* arr = (int*)malloc(n * sizeof(int));
printf("\nEnter the elements in the array:\n");
for (int i = 0; i < n; i++)
scanf("%d", arr + i);
combsort(arr, n);
printf("Sorted array: \n");
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
return 0;
}