Skip to content

Instantly share code, notes, and snippets.

@syedsouban
Created July 11, 2018 07:14
Show Gist options
  • Save syedsouban/230ad57f79b81a90812f9ba4d275d8da to your computer and use it in GitHub Desktop.
Save syedsouban/230ad57f79b81a90812f9ba4d275d8da to your computer and use it in GitHub Desktop.
#include<stdio.h>
void swap(int* x,int* y)
{
int t;
t=*x;
*x=*y;
*y=t;
}
int partition(int* a,int start,int end)
{
int pivot=a[end];
int pIndex=start,i,t;
for(i=start;i<end;i++)
{
if(a[i]<=pivot)
{
swap(&a[i],&a[pIndex]);
pIndex++;
}
}
swap(&a[end],&a[pIndex]);
return pIndex;
}
void QuickSort(int *a, int start,int end)
{
if(start>=end) return;
int pIndex=partition(a,start,end);
QuickSort(a,start,pIndex-1);
QuickSort(a,pIndex+1,end);
}
main()
{
int n;
scanf("%d",&n);
int a[n];
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
QuickSort(a,0,n-1);
for(int i=0;i<n;i++)
printf("%d ",a[i]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment