-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqucik_sort.c
58 lines (46 loc) · 853 Bytes
/
qucik_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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void swap(int *a, int *b){
int temp = *a;
*a = *b;
*b = temp;
}
int partition(int a[], int low, int hi){
int i = low;
int j = hi;
int pivot = a[low];
while(i <j){
while(i < hi && a[i] <= pivot)
i++;
while(a[j] > pivot)
j--;
if(i < j)
swap(&a[i], &a[j]);
}
swap(&a[low], &a[j]);
return j;
}
void quick_sort(int a[], int low, int hi){
if(low < hi){
int pivot = partition(a, low, hi);
quick_sort(a, low, pivot-1);
quick_sort(a, pivot+1, hi);
}
}
int main(int argc, char **argv)
{
int a[100];
int i = 0;
srand(time(0));
int size = sizeof(a)/sizeof(a[0]);
for(i=0;i<size;i++){
a[i] = rand()%1000;
printf("%d\t", a[i]);}
printf("\n\n\n");
quick_sort(a, 0, size-1);
for(i=0;i<size;i++)
printf("%d\t", a[i]);
printf("\n");
return 0;
}