Skip to content

Instantly share code, notes, and snippets.

@suryansh011
Created October 1, 2021 18:18
Show Gist options
  • Save suryansh011/a1b7ad07caece01d89f03fa37ba14477 to your computer and use it in GitHub Desktop.
Save suryansh011/a1b7ad07caece01d89f03fa37ba14477 to your computer and use it in GitHub Desktop.
Simple Quick Sort
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