-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcycleSort.c
47 lines (36 loc) · 1.21 KB
/
cycleSort.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
#include "insertion.h"
// Worst case performance О(n**2) comparisons
// Best case performance O(n**2) comparisons
// Average performance О(n**2) comparisons
void cycleSort(long int *array, int length){
long int aux, swap, *i, *j, *k, *pos;
for(i = array; i < array + length - 2; i++){
aux = *i;
pos = i;
for(j = i + 1; j < array + length; j++) // Find the right position
if(*j < aux)
pos++;
if(pos == i) // If the element is in correct position
continue;
while(aux == *pos) // For duplicate elements
pos++;
if(pos != i){ // Setting the element in correct position
swap = aux;
aux = *pos;
*pos = swap;
}
while(pos != i){ // Rotating the cycle
pos = i;
for(k = i + 1; k < array + length; k++) // Find the right position
if(*k < aux)
pos++;
while(aux == *pos) // For duplicate elements
pos++;
if(aux != *pos){ // Setting the element in correct position
swap = aux;
aux = *pos;
*pos = swap;
}
}
}
}