Skip to content

Instantly share code, notes, and snippets.

@pgampe
Created October 28, 2012 02:17
Show Gist options
  • Save pgampe/3967199 to your computer and use it in GitHub Desktop.
Save pgampe/3967199 to your computer and use it in GitHub Desktop.
Info/Uni Bonn/WS2012-2012/Algo I/Blatt 2/Aufgabe 5
#include <stdlib.h>
#include <stdio.h>
void printA(int A[10]) {
int i = 0;
for (i = 0; i < 10; i++) {
printf("%i,", A[i]);
}
printf("\n");
}
int part(int A[10], int l, int r) {
int x = 0;
int i = 0;
int j = 0;
int z = 0;
i = l - 1;
x = A[r - 1];
printf("part, l = %i, r = %i, x = %i \n", l, r, x);
printA(A);
for (j = l; j < r; j++) {
if (A[j - 1] <= x) {
i++;
printf("vertausche bei if %i mit %i \n", A[i - 1], A[j - 1]);
z = A[i - 1];
A[i - 1] = A[j - 1];
A[j - 1] = z;
printA(A);
}
}
printf("vertausche am Ende %i mit %i \n", A[i], A[r - 1]);
z = A[i];
A[i] = A[r - 1];
A[r - 1] = z;
printA(A);
return (i + 1);
}
void qsreg(int A[10], int l, int r) {
int p = 0;
printf("qsreg, l = %i, r = %i \n", l, r);
if (l < r) {
p = part(A, l, r);
qsreg(A, l, p-1);
qsreg(A, p+1, r);
}
}
int main() {
int A[10] = {1,3,6,9,5,7,8,2,4,10};
printA(A);
qsreg(A, 1, 10);
printA(A);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment