Beispiel #1
0
static void
lomuto_qsort(int arr[], int left, int right) {
    if (left < right) {
        int mid = lomuto_partition(arr, left, right);
        lomuto_qsort(arr, left, mid-1);
        lomuto_qsort(arr, mid+1, right);
    }
}
Beispiel #2
0
/* median of 3 partition scheme
 * */
static int
median_of_three_partition(int arr[], int left, int right) {
    int mid = left + (right - left) / 2;

    if (arr[left] >= arr[mid] && arr[left] <= arr[right]) {
        swap(arr, left, right);
    } else if (arr[mid] >= arr[left] && arr[mid] <= arr[right]) {
        swap(arr, mid, right);
    }

    return lomuto_partition(arr, left, right);
}
Beispiel #3
0
void _quicksort(std::vector<T>& arr, const int64_t lo, const int64_t hi) {
#if 0
  if (lo < hi) {
    int64_t mid = lomuto_partition(arr, lo, hi);
    _quicksort(arr, lo, mid-1);
    _quicksort(arr, mid+1, hi);
  }
#else
  if (lo < hi) {
    int64_t mid = hoare_partition(arr, lo, hi);
    _quicksort(arr, lo, mid);
    _quicksort(arr, mid+1, hi);
  }
#endif
}
static void
quick_sort_with_stack(int arr[], int left, int right) {
    struct stack* s = stack_new();

    stack_push(s, left, right);

    while (! stack_empty(s)) {
        int l;
        int r;

        stack_pop(s, &l, &r);

        if (l < r) {
            int m = lomuto_partition(arr, l, r);

            stack_push(s, l, m-1);
            stack_push(s, m+1, r);
        }
    }

    stack_del(s);
}
Beispiel #5
0
/* Random partition scheme
 * */
static int
randomized_partition(int arr[], int left, int right) {
    int r = rand_num(left, right);
    swap(arr, r, right);
    return lomuto_partition(arr, left, right);
}