Skip to content

Instantly share code, notes, and snippets.

@manojnaidu619
Last active July 1, 2019 17:31
Show Gist options
  • Save manojnaidu619/e77061fad36c9bbb2b2a401cd3b82655 to your computer and use it in GitHub Desktop.
Save manojnaidu619/e77061fad36c9bbb2b2a401cd3b82655 to your computer and use it in GitHub Desktop.
Quick Sort program in C
// Refer to these video tutorials :
// https://www.youtube.com/watch?v=MZaf_9IZCrc
// and
// https://www.youtube.com/watch?v=T3Paafdqcsw
#include <stdio.h>
void quick(int a[], int first, int last){
if(first<last){
int temp;
int i = first;
int j = last;
int pivot = first;
while(i<j){
while(a[i] <= a[pivot]){
i++;
}
while(a[j] > a[pivot]){
j--;
}
if(i<j){
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
temp = a[j];
a[j] = a[pivot];
a[pivot] = temp;
quick(a,first,j-1);
quick(a,j+1,last);
}
}
int main(){
int n, a[100];
printf("Enter size: \n");
scanf("%d",&n);
printf("Enter %d elements : \n",n);
for(int i=0;i<n;i++){
scanf("%d",&a[i]);
}
quick(a,0,n-1);
printf("Sorted array : ");
for(int i=0;i<n;i++){
printf("%d ",a[i]);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment