Created
October 1, 2021 18:18
-
-
Save suryansh011/a1b7ad07caece01d89f03fa37ba14477 to your computer and use it in GitHub Desktop.
Simple Quick Sort
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> | |
int partition(int s[], int l, int h) { | |
int i, p, firsthigh, temp; | |
p = h; | |
firsthigh = l; | |
for (i = l; i < h; i++) { | |
if (s[i] < s[p]) { | |
temp = s[i]; | |
s[i] = s[firsthigh]; | |
s[firsthigh] = temp; | |
firsthigh++; | |
} | |
} | |
temp = s[p]; | |
s[p] = s[firsthigh]; | |
s[firsthigh] = temp; | |
return firsthigh; | |
} | |
void quicksort(int s[], int l, int h) { | |
int p; /* index of partition */ | |
if (l < h) { | |
p = partition(s, l, h); | |
quicksort(s, l, p - 1); | |
quicksort(s, p + 1, h); | |
} | |
} | |
int main() { | |
int i; | |
int n; /* size of the array */ | |
scanf("%d", &n); | |
int s[n]; | |
for (i = 0; i < n; i++) | |
scanf("%d", &s[i]); | |
quicksort(s, 0, n-1); | |
for (i = 0; i < n; i++) | |
printf("%d ", s[i]); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment