-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathquicksort.c
48 lines (47 loc) · 1.07 KB
/
quicksort.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
#include <stdio.h>
#include <stdlib.h>
int quicksort(int*a, int len){
quicksort_r(a,0,len-1);
return 0;
}
int quicksort_r(int* a,int start,int end){
if (start>=end) {
return 0;
}
int pivot=a[end];
int swp;
//set a pointer to divide array into two parts
//one part is smaller than pivot and another larger
int pointer=start;
int i;
for (i=start; i<end; i++) {
if (a[i]<pivot) {
if (pointer!=i) {
//swap a[i] with a[pointer]
//a[pointer] behind larger than pivot
swp=a[i];
a[i]=a[pointer];
a[pointer]=swp;
}
pointer++;
}
}
//swap back pivot to proper position
swp=a[end];
a[end]=a[pointer];
a[pointer]=swp;
quicksort_r(a,start,pointer-1);
quicksort_r(a,pointer+1,end);
return 0;
}
int main(){
int a[10]={2,1,4,7,5,8,9,20,0,3};
int len=10;
quicksort(a,len);
int i;
for (i=0; i<len; i++) {
printf("%d\t",a[i]);
}
printf("\n");
return 0;
}