-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomb_sort.c
64 lines (54 loc) · 1.02 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
62
63
64
#include <stdio.h>
#include <math.h>
/*
Comb Sort
==============
https://en.wikipedia.org/wiki/Comb_sort
Worst-case performance: O(n2)
Best-case performance: O(n log n)
Average performance: O(n2/2^p)
Worst-case space complexity: O(1)
Stable: No
Features
--------
- Eliminates turtles, or small values near the end of the list.
- shrink factor of 1.3 is considered optimal.
*/
void comb_sort(int arr[], int length);
// comb sort on an array
void comb_sort(int arr[], int length)
{
int sorted = 0;
// initialise gap size
int gap = length;
// set shrink factor
double shrink = 1.3;
while (sorted == 0)
{
// update gap value for next comb
int gap = floor(gap / shrink);
if (gap > 1)
{
// not sorted until gap size is zero
sorted = 0;
}
else
{
gap = 1;
sorted = 1;
}
// a single "comb" over the input list
int i = 0;
while (i + gap < length - 1)
{
if (arr[i] > arr[i + gap])
{
int temp = arr[i];
arr[i] = arr[i + gap];
arr[i + gap] = temp;
sorted = 0;
}
i++;
}
}
}