-
Notifications
You must be signed in to change notification settings - Fork 0
/
quicksort.cpp
53 lines (53 loc) · 976 Bytes
/
quicksort.cpp
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
/*
* To sort an array in ascending order using quicksort algorithm
*/
#include<stdio.h>
void quicksort(int A[],int p,int r);
int partition(int A[],int p,int r);
void swap(int A[],int i,int j);
int main()
{
int A[]={6,9,1,45,9,2,89,77,4,5};
int i,n;
n=sizeof(A)/sizeof(A[0]);
quicksort(A,0,n-1);
printf("Array sorted in ascending order is as follows:\n");
for(i=0;i<n;i++)
{
printf("%d\n",A[i]);
}
return 0;
}
void quicksort(int A[],int p,int r)
{
int q;
if(p<r)
{
q=partition(A,p,r);
quicksort(A,p,q-1);
quicksort(A,q+1,r);
}
}
int partition(int A[],int p,int r)
{
int x,i,j;
x=A[r];
i=p-1;
for(j=p;j<r;j++)
{
if(A[j]<=x)
{
i=i+1;
swap(A,i,j);
}
}
swap(A,i+1,r);
return i+1;
}
void swap(int A[],int i,int j)
{
int temp;
temp=A[i];
A[i]=A[j];
A[j]=temp;
}