Created
September 21, 2016 11:09
-
-
Save devils-ey3/4bf2f21670eb75f56a64228cbf7670cb to your computer and use it in GitHub Desktop.
Quick sort by C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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