-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuick.cpp
More file actions
61 lines (56 loc) · 753 Bytes
/
Quick.cpp
File metadata and controls
61 lines (56 loc) · 753 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
50
51
52
53
54
55
56
57
58
59
/* quick sort */
#include<stdio.h>
void quick(int [],int,int);
int i,n;
int main()
{
int i,a[100],n;
printf("enter the no of elements ");
scanf("%d",&n);
printf("enter the elements\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
quick(a,0,n-1);
printf("after sorting the array is\n");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
return(0);
}
void quick(int a[],int f,int l)
{
int pivot,temp,low,high;
low=f;high=l;
pivot=a[(low+high)/2];
do
{
while(a[low]<pivot)
{
low++;
}
while(pivot<a[high])
{
high--;
}
if(low<=high)
{
temp=a[low];
a[low]=a[high];
a[high]=temp;
low++;
high--;
}
}while(low<=high);
if(f<high)
{
quick(a,f,high);
}
if(low<l)
{
quick(a,low,l);
}
printf("\n");
}