开发者

Indexed array quicksort debug

开发者 https://www.devze.com 2023-03-14 18:39 出处:网络
I\'m going nuts over this special quicksort algorithm and I don\'t know where is the problem. I\'ve found an example on a forum that explains very well 开发者_开发技巧what I\'m trying to do.

I'm going nuts over this special quicksort algorithm and I don't know where is the problem. I've found an example on a forum that explains very well 开发者_开发技巧what I'm trying to do.

Given an index array of contiguous, ordered numbers (representing elements in the data array), you still have to compare the data values; you just access them via the index. You swap the index values, however, not the data values.

Unsorted data: 09 04 47 05 03 12
Index array:   00 01 02 03 04 05

After sort,
Unsorted data: 09 04 47 05 03 12
Index array:   04 01 03 00 05 02

Print the values indexed by the index array,
from beginning to end of the array:

Index array [0] value [04] data array [04] = 03
            [1]        01             [01]   04
            [2]        03             [03]   05
            [3]        00             [00]   09
            [4]        05             [05]   12
            [5]        02             [02]   47

Output is the data, ordered at the output

I only add one thing. The comparator is a normal comparator with the exception that if we are comparing two different characteres with same values, we compare the next character of each and return that result. That is, comparing the rotations.

int compare(unsigned char i, unsigned char j);

I won't post the definition because I am sure that it works perfect. The bug lies in the quicksort definition.

void quicksort(unsigned char* a, unsigned char left, unsigned char right) {
    unsigned char i = left;
    unsigned char j = right;
    unsigned char half = (left + right) / 2;
    while (i <= j) {
        while ((compare(a[i], a[half]) == -1) && (i < right))
            i++;
        while ((compare(a[j], a[half]) == 1) && (j > left))
            j--;

        if (i <= j) {
            unsigned char aux = a[i];
            a[i] = a[j];
            a[j] = aux;
            i++;       //THERE
            j--;       //THERE
        }

    }

    if (left < j)
        quicksort(a, left, j);
    if (i < right)
        quicksort(a, i, right);

}

Sometimes i= 255 and j=0, and I don't why, program gets to THERE, and their values go overflow. I've looked for bugs a thousand times and I can't find where is the mistake.

Notes: 1) I am perfectly aware of C++ unsigned char ranges, and I can't change them to int. 2) I don't actually include the declaration of the actual data array. The compare function has access to it since it is an atribute of its class.


Uhh you can't store numbers larger than 255 or lower than 0 in an unsigned char. An unsigned char can store the range defined by one unsigned byte, so 8 binary digits. 2^8 = 256, and since we include 0, we have 256 - 1, giving us 255. (or in hex, 0xFF)

Try using ints, or even shorts. (keyword short)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号