void quick_sort_range(int a[], int first, int last) {
    if (last <= first) return;  // length is <= 1

    int pivot = a[first];       // first element is the pivot
    int pos = last;             // where to put next larger

    for (int i = last; i > first; --i) {
      if (a[i] > pivot) {
        swap(&a[pos], &a[i]);
        --pos;
      }
    }
    swap(&a[first], &a[pos]);   // put pivot in correct place
    quick_sort_range(a, first, pos - 1);
    quick_sort_range(a, pos + 1, last);
  }

  void quick_sort(int a[], int len) {
    quick_sort_range(a, 0, len - 1);
  }