forked from laviii123/Btecky
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Quick Sort
52 lines (46 loc) · 1.05 KB
/
Quick Sort
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>
int partition (int a[], int start, int end)
{
int pivot = a[end];
int i = (start - 1);
for (int 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 = partition(a, start, end);
quick(a, start, p - 1);
quick(a, p + 1, end);
}
}
int main()
{
int n,i;
printf("Enter the number of elements : ");
scanf("%d",&n);
int a[n];
for(int i=0; i<5; i++){
scanf("%d",&a[i]);
}
printf("Before sorting array elements are - \n");
for (i = 0; i < n; i++)
printf("%d ", a[i]);
quick(a, 0, n - 1);
printf("\nAfter sorting array elements are - \n");
for (i = 0; i < n; i++)
printf("%d ", a[i]);
return 0;
}