Skip to content

Instantly share code, notes, and snippets.

@tahia-khan tahia-khan/qsort.c
Created Nov 5, 2015

Embed
What would you like to do?
#include <stdio.h>
void qsort(int *X, int L, int U) {
int T, M, i, tmp;
if (L < U) {
T = X[L];
M = L;
for (i = L+1; i < U; i++) {
if (X[i] < T) {
M += 1;
tmp = X[M];
X[M] = X[i];
X[i] = tmp;
}
}
X[L] = X[M];
X[M] = T;
qsort(X, L, M);
qsort(X, M+1, U);
}
}
int main(int argc, char *argv[]) {
int a[] = {4, 65, 2, -31, 0, 99, 2, 83, 782, 1};
int n = sizeof a / sizeof a[0];
int i;
for (i = 0; i < n; i++)
printf("%d%s", a[i], i == n - 1 ? "\n" : " ");
qsort(a,0, n);
for (i = 0; i < n; i++)
printf("%d%s", a[i], i == n - 1 ? "\n" : " ");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.