-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path29.Quick sort.c
73 lines (62 loc) · 1.01 KB
/
29.Quick sort.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
#include<stdio.h>
void quicksort(int[],int,int);
int partition (int[],int,int);
void main()
{
int lb,ub,a[10],i,n;
printf("Enter the total no of elements : ");
scanf("%d",&n);
printf("\nEnter the unsorted array : ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
lb=0;
ub=n-1;
quicksort(a,lb,ub);
printf("\n\nArray elements after sorting : ");
for(i=0;i<n;i++)
{
printf("\n%d",a[i]);
}
}
void quicksort(int a[10],int lb,int ub)
{
if(lb<ub)
{
printf("\nlb = %d \nub= %d\n",lb,ub);
int loc=partition(a,lb,ub);
quicksort(a,lb,loc-1);
quicksort(a,loc+1,ub);
}
}
int partition(int a[10],int lb,int ub)
{
int pivot=a[lb];
int start=lb;
int end=ub;
while (start < end)
{
while(a[start]<=pivot)
{
start++;
}
while(a[end]>pivot)
{
end--;
}
if(start<end)
{
swap(&a[start],&a[end]);
}
}
swap(&a[lb],&a[end]);
printf("\nLocation = %d\n",end+1);
return end;
}
void swap(int *a,int *b)
{
int t = *a;
*a = *b;
*b = t;
}