-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuickSort.c
57 lines (51 loc) · 986 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
44
45
46
47
48
49
50
51
52
53
54
55
56
#include<stdio.h>
Quick_Sort(int a[20],int low,int high)
{
int j;
if(low<high)
{
j=partition(a[20],low,high);
Quick_Sort(a[20],low,j-1);
Quick_Sort(a[20],j+1,high);
}
}
int partition(int a[],int low,int high)
{
int pivot=a[low];
int i=low;
int j=high+1;
do
{
do
{
i++;
}while(pivot>a[i]);
do
{
j--;
}while(pivot<a[i]);
if(j>i)
{
a[i]=a[i]+a[j];
a[j]=a[i]-a[j];
a[i]=a[i]-a[j];
}
}while(j>i);
a[low]=a[low]+a[j];
a[j]=a[low]-a[j];
a[low]=a[low]-a[j];
return j;
}
main()
{
int a[20],n,i,j,k,low,high;
printf("Enter Array size :");
scanf("%d",&n);
printf("Enter Array Elements : ");
for(k=0;k<n;k++)
scanf("%d",&a[k]);
Quick_Sort(a[20],0,n-1);
printf("Array after Sorting are as follows:\n");
for(k=0;k<n;k++)
printf("%d",a[k]);
}