-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquicksort.c
57 lines (53 loc) · 987 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
57
#include<stdio.h>
void quicksort(int A[50],int first,int last)
{
int i, j, pivot, temp;
if(first<last)
{
pivot=first;
i=first;
j=last;
while(i<j)
{
while(A[i]<=A[pivot] && i<last)
i++;
while(A[j]>A[pivot])
j--;
if(i<j)
{
temp=A[i];
A[i]=A[j];
A[j]=temp;
}
}
temp=A[pivot];
A[pivot]=A[j];
A[j]=temp;
quicksort(A,first,j-1);
quicksort(A,j+1,last);
}
}
void display(int A[50],int n)
{
for(int i=0;i<n;i++)
{
printf("%d ",A[i]);
}
printf("\n");
}
void main()
{
int A[50],n;
printf("Enter the no of elements you want to enter\n");
scanf("%d",&n);
printf("Enter array elements\n");
for(int i=0;i<n;i++)
{
scanf("%d",&A[i]);
}
printf("Original array: \n");
display(A,n);
quicksort(A,0,n-1);
printf("\nThe sorted array is: \n");
display(A,n);
}