Can't sort the sort quickly?

Approach 1

CAR Hoare introduced the partitioning logic (shown below) taught at school,

low = pivot = 0;
i = 1;
j = high = listSize-1;

while (true) {
    while (a[i] <= a[pivot] && (i < high)) {
        i = i + 1;
    }
    while (a[j] >= a[pivot] && (j > low)) {
        j = j - 1; 
    }

    if (i >= j)
        break;
    swap(a[i], a[j])
}

swap(a[j], a[pivot]); // pivot element is positioned(once)
return j;

Approach 2

In try to make it stable sorting , instead of jpointing to the last index ( listSize-1), if jpointing to listSize/2(i.e. mid), then,

we find ourselves in a scenario where j > highor i >= midwhere a[i]do not have the appropriate a[j]replacement and vice versa. In this case, replacing a[i]with a[pivot]also does not make sense , which looks incorrect. . To confirm the same,


My question is:

With approach 2,

Keeping the essence of quicksort, can we not split the pivot element (at any index)?

. , .

+2
1

, :

  • , , 2 .

  • .

, , :

  • .
  • quick-sort , :

    int sortptr(const void *a, const void *b) {
        const my_type * const *pa = a;
        const my_type * const *pb = b;
        int cmp = original_compare_function(*pa, *pb);
        return cmp ? cmp : (pa > pb) - (pa < pb);
    }
    
  • .

, , , . merge-sort , .

+6

Source: https://habr.com/ru/post/1667204/


All Articles