-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquicksort.c
79 lines (70 loc) · 1.6 KB
/
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include<stdio.h>
int partion(int [10],int,int);
void arraysort(int [10],int,int);
int main()
{
int left=0,i,right,n,a[10];
printf("enter total no of elements in array ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n enter %d element ",i);
scanf("%d",&a[i]);
}
right=n-1;
quicksort(a,left,right);
//array printing
for(i=0;i<n;i++)
{
printf("%5d",a[i]);
}
return 0;
}
int partition(int a[10],int left,int right)
{
int i,loc,temp;/*n=no of elements,l=lowerbound,r=upperbound,loc=location of pivot element,temp=tempvariable for swapping*/
loc=left;
printf("\nright set\n ");
while(left<right)
{
printf("\n in while \n");
while(a[loc]<=a[right] && loc<right)
{
printf("\n in condition 1 \n");
right=right-1;
}
if(a[loc]>a[right])
{
temp=a[right];
a[right]=a[loc];
a[loc]=temp;
loc=right;
left=left+1;
}
while(a[loc]>=a[left] && loc>left)
{
left=left+1;
}
if(a[loc]<a[left])
{
temp=a[left];
a[left]=a[loc];
a[loc]=temp;
loc=left;
right=right-1;
}
}
a[left]=a[right];
a[right]=a[loc];
return right;
}
void quicksort( int a[], int left, int right )
{
if ( right > left )
{
int pivot;
pivot= partition( a, left, right );
quicksort( a, left, pivot-1 );
quicksort( a, pivot+1, right );
}
}