#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 k;

  if(l >= r) return;

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

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

  while(1)
  {
    do { ++i; } while (a[i] < pivot);
    do { --k; } while (a[k] > pivot);

    if (i < k)
    {
      swap(&a[i], &a[k]);
    }
    else
    {
      return k;
    }
  }
}

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;
}

