-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqsort.c
More file actions
49 lines (45 loc) · 883 Bytes
/
qsort.c
File metadata and controls
49 lines (45 loc) · 883 Bytes
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
#include<stdio.h>
void swap(int* a, int*b){
int t = *a;
*a = *b;
*b = t;
}
int partition(int a[], int l, int h){
int x, i, temp;
x = a[h];
i = l - 1;
for(int j = l; j <= h - 1; j++){
if(a[j] <= x){
i++;
swap(&a[i], &a[j]);
}
}
swap(&a[i+1], &a[h]);
return (i + 1);
}
void Quicksort(int a[], int l, int h){
int q;
if(l < h){
q = partition(a, l, h);
Quicksort(a, l, q - 1);
Quicksort(a, q + 1, h);
}
}
int main(){
printf("Enter array length: ");
int len, i;
scanf("%d", &len);
int a[50];
printf("Enter array elements: ");
for(i = 0; i < len; i++)
scanf("%d", &a[i]);
printf("\nEntered array: ");
for(i = 0; i < len; i++)
printf("%d\t", a[i]);
printf("\n");
printf("Array after sorting: ");
Quicksort(a, 0, len - 1);
for(i = 0; i < len; i++)
printf("%d\t", a[i]);
return 0;
}