-
Notifications
You must be signed in to change notification settings - Fork 0
/
combsort.c
44 lines (42 loc) · 1.09 KB
/
combsort.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
#include "combsort.h"
void comb_sort(data array[], int len) {
int gap = len;
int swaps = 0; // 1 if a swap has occurred, 0 if none
int i; // index used to scan the list
data tmp; // temporary variable used to swap elements
while (gap > 1 || swaps) {
if (gap > 1) // update the gap value
gap /= 1.247330950103979;
swaps = 0; // reset the swaps flag
for (i = 0; i+gap < len; i++)
if (array[i] > array[i+gap]) {
tmp = array[i];
array[i] = array[i+gap];
array[i+gap] = tmp;
swaps = 1;
}
}
}
void comb_sort_debug(data array[], int len, STAT *stat) {
int gap = len;
int swaps = 0; // 1 if a swap has occurred, 0 if none
int i; // index used to scan the list
data tmp; // temporary variable used to swap elements
stat->space++;
stat->allo++;
while (gap > 1 || swaps) {
if (gap > 1) // update the gap value
gap /= 1.247330950103979;
swaps = 0; // reset the swaps flag
for (i = 0; i+gap < len; i++) {
if (array[i] > array[i+gap]) {
tmp = array[i];
array[i] = array[i+gap];
array[i+gap] = tmp;
swaps = 1;
stat->swap++;
}
stat->comp++;
}
}
}