#include <stdio.h>

#define ARRAYLEN 10

void quickSort(int * a, const int l, const int r);
int partition(int * a, int l, int r);

// allgemeine Array-Werkzeuge
void copyArray(int * source, int * dest, const int len);
void printArray(const int * a, const int l, const int r);
void swap(int * a, int * b);

void quickSort(int * a, const int l, const int r)
{
  int m;
  if (l>=r) return;

  m = partition(a, l, r);
  quickSort(a, l, m-1);
  quickSort(a, m+1, r);
}

int partition(int * a, const int l, const int r)
{
  int m=r, i=l, k=r-1;
  int pivot = a[m];

  while (1)
  {
    while (a[i] <= pivot && i<r) ++i;
    while (a[k] >= pivot && k>l) --k;

    if (i>=k) break;

    swap(&a[i], &a[k]);
  }
  swap(&a[i], &a[m]);
  return i;
}

void copyArray(int * source, int * dest, const int len)
{
  int i;
  for (i=0; i<len; ++i)
  {
    dest[i] = source[i];
  }
}

void printArray(const int * a, const int l, const int r)
{
  int i;
  for (i=l; i<=r; ++i)
  {
    printf(" %d", a[i]);
  }
  printf("\n");
}

void swap(int * a, int * b)
{
  if (a==b) return;
  *a ^= *b;
  *b ^= *a;
  *a ^= *b;
}

int main(void) {
  int unordnung[ARRAYLEN] = { 4, 7, 2, 5, 1, 9, 3, 8, 6, 0 };

  printArray(unordnung, 0, ARRAYLEN-1);
  quickSort(unordnung, 0, ARRAYLEN-1);
  printArray(unordnung, 0, ARRAYLEN-1);

  return 0;
}

