Last active
July 1, 2019 17:31
-
-
Save manojnaidu619/e77061fad36c9bbb2b2a401cd3b82655 to your computer and use it in GitHub Desktop.
Quick Sort program in C
This file contains hidden or 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
// 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