Skip to content

Commit

Permalink
Create quicksort.c
Browse files Browse the repository at this point in the history
quick sort using recursion in c
  • Loading branch information
PaladuguLekhaRavali authored Oct 28, 2023
1 parent e5dad3f commit 54c36b4
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions data_structures/array/quicksort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include<stdio.h>
void quick( int a[],int first,int last){
int i,j,pivot,t;
if(first<last)
{
i=first;
j=last;
pivot=first;
while(i<j)
{
while(a[i]<=a[pivot]&&i<=last)
i++;
while(a[j]>a[pivot])
j--;
if(i<j)
{t=a[i];
a[i]=a[j];
a[j]=t;

}
}
t=a[pivot];
a[pivot]=a[j];
a[j]=t;
quick(a,first,j-1);
quick(a,j+1,last);
}
}
void main()
{
int n,i,j,key,t;
printf("Enter the size of the array");
scanf("%d",&n);
int a[n];
printf("Enter the elements to the array");
for(i=0;i<n;i++)
{

scanf("%d",&a[i]);}
printf("Elements before sorting are");
for(i=0;i<n;i++)
{

printf("%d ",a[i]);}
printf("Elements aftere sorting are");
quick(a,0,n-1);
for(i=0;i<n;i++)
{

printf("%d ",a[i]);}

}

0 comments on commit 54c36b4

Please sign in to comment.