-
Notifications
You must be signed in to change notification settings - Fork 0
/
quick.c
66 lines (60 loc) · 1.18 KB
/
quick.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
58
59
60
61
62
63
64
65
66
#include <stdio.h>
int a[10], n;
void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
void print_arr(int a[],int n)
{
for(int i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
printf("\n");
}
void quick_sort(int low, int high, int a[])
{
if (low < high)
{
int pivot = a[low];
int i = low+1;
int j = high;
while (i <= j)
{
while (i <= high && a[i] <= pivot)
{
i++;
}
while (a[j] >= pivot && j > low)
{
j--;
}
if(i<j)
{
swap(&a[i], &a[j]);
}
}
swap(&a[low], &a[j]);
quick_sort(low, j - 1, a);
quick_sort(j + 1, high, a);
}
}
void main()
{
printf("Enter the no of elements in the array : ");
scanf("%d", &n);
printf("Enter the elements\n");
for (int i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
printf("The unsorted array is : ");
print_arr(a, n);
int low = 0;
int high = n - 1;
quick_sort(low, high, a);
printf("The sorted array is : ");
print_arr(a,n);
}