Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

FUCK YEAH BUBBLE SORT

Name: Anonymous 2008-07-09 1:39


inline void swap_bytes(void *a, void *b, size_t size)
{
    char *a_char = a, *b_char = b, temp;

    while(size--) {
        temp      = *a_char;
        *a_char++ = *b_char;
        *b_char++ = temp;
    }
}

void bubble_sort(void *base, size_t num, size_t size, int (*comparator)(const void *, const void *))
{
    void *first, *second;
    size_t pass, passes = num - 1, i;
    int result;

    if(num > 1) {
        for(pass = 0; pass < passes; ++pass) {
            second = base + size * num - size;
            first  = second - size;

            for(i = num; i > pass + 1; --i) {
                result = comparator(first, second);
                if(result == 1)
                    swap_bytes(first, second, size);
                first -= size;
                second -= size;
            }
        }
    }
}


Here you go, /prog/, a generic implementation of bubble sort that can replace qsort().

Name: Anonymous 2008-07-10 6:06

>>15
try.c: In function ‘sort’:
try.c:13: error: invalid operands to binary | (have ‘void *’ and ‘__pid_t’)

FAIL.

Also: fork is not a standard function; 1 is not a portable exit status; CHAR_BIT % 2 is insufficient since sizeof (uintmax_t) can be even, in which case you've just overwritten the sort results; you really shouldn't finalize temp before the loop is done.

Summary:
Of 12 issues you have
- fixed 8
- tried to fix (but failed) 3
- introduced another 5
In addition, uintmax_t i is morally wrong.

Conclusion:
Issues 5 and 6b (the bugs that prevent this code from working in practice) are still present. New issues include use of freed memory and double free. And it still doesn't even compile.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List