-
Notifications
You must be signed in to change notification settings - Fork 0
/
QuickSort.c
43 lines (43 loc) · 991 Bytes
/
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
#include <stdio.h>
int part(int a[], int start, int end)
{
int pivot = a[end];
int i = (start - 1), j;
for (j = start; j <= end - 1; j++){
if (a[j] < pivot){
i++;
int t = a[i];
a[i] = a[j];
a[j] = t;
}
}
int t = a[i + 1];
a[i + 1] = a[end];
a[end] = t;
return (i + 1);
}
void quick(int a[], int start, int end){
if (start < end){
int p = part(a, start, end);
quick(a, start, p - 1);
quick(a, p + 1, end);
}
}
void printArr(int a[], int n){
int i;
for (i = 0; i < n; i++)
printf("%d ", a[i]);
}
void main(){
int a[10], n, i;
printf("Enter the number of elements in the array\n");
scanf("%d", &n);
printf("Enter the elements of the array\n");
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
printf("Before sorting array elements are - \n");
printArr(a, n);
quick(a, 0, n - 1);
printf("\nAfter applying Quick Sort array elements are - \n");
printArr(a, n);
}