Skip to content

Instantly share code, notes, and snippets.

@devils-ey3
Created September 21, 2016 11:09
Show Gist options
  • Save devils-ey3/4bf2f21670eb75f56a64228cbf7670cb to your computer and use it in GitHub Desktop.
Save devils-ey3/4bf2f21670eb75f56a64228cbf7670cb to your computer and use it in GitHub Desktop.
Quick sort by C
#include <stdio.h>
void quicksort(int a[],int first,int last)
{
int temp,pviot,i,j;
if (first<last)
{
pviot = first;
i = first ;
j = last;
while (i<j){
while (a[i]<=a[pviot] && i<last)
i++;
while (a[j]>a[pviot])
j--;
if (i<j){
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
temp = a[pviot];
a[pviot] = a [j];
a[j] = temp;
quicksort(a,first,j-1);
quicksort(a,j+1,last);
}
}
int main(){
int x[20],size,i;
printf("Enter size of the array: ");
scanf("%d",&size);
printf("Enter %d elements: ",size);
for(i=0;i<size;i++)
scanf("%d",&x[i]);
quicksort(x,0,size-1);
printf("Sorted elements: ");
for(i=0;i<size;i++)
printf(" %d",x[i]);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment