-
Notifications
You must be signed in to change notification settings - Fork 0
/
qsort.c
68 lines (56 loc) · 1.15 KB
/
qsort.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
65
66
67
#include <stdio.h>
void hlSwap(int i, int j);
void qsort(int v[], int left, int right);
void printArr(int v[]);
int main(){
int test[10] = {10,5,8,3,9,4,1,2,7,6};
qsort(test, 0, 9);
printf("Done.\n ");
printArr(test);
return 0;
}
void qsort(int v[], int left, int right){
printf("enter qsort, left: %d, right %d, ",left,right);
printArr(v);
int i, last;
void swap(int v[], int i, int j);
if(left >= right) // less than 2 elem, do nothing
return;
swap(v, left, (left + right)/2); // move partition elem
last = left;
for( i = left+1; i <= right; i++)
if(v[i] < v[left])
swap(v, ++last, i);
swap(v, left, last);
qsort(v, left, last-1);
qsort(v, last+1, right);
}
void swap(int v[], int i, int j){
int temp;
temp = v[i];
v[i] = v[j];
v[j] = temp;
printf("Swap( %d, %d) - indices: %d, %d\n", v[i], v[j],i, j);
hlSwap(i, j);
printArr(v);
}
void printArr(int v[]){
int i;
printf("Array: ");
for(i = 0; i < 10; i++)
printf("%d,",v[i]);
printf("\n");
}
void hlSwap(int k, int j){
if(k == j)
return;
int i;
printf(" ");
for(i = 0; i < 10; i++){
if(i == k || i == j)
printf("S ");
else
printf(" ");
}
printf("\n");
}