HiveBrain v1.2.0
Get Started
← Back to all entries
snippetcMinor

Quicksort using pointers

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
pointersusingquicksort

Problem

As an exercise, I've written quicksort algorithm in C using pointers. Please comment and help me find the cases where it breaks (if any).

void qsort(int *, int, int);
void swap(int *, int *);

void qsort(int *v, int left, int right)
{
    int i, *last;

    if (right = pivot
}

void swap(int *i, int *j)
{
    int temp;

    temp = *i;
    *i = *j;
    *j = temp;
}

Solution

I'd make right an exclusive instead of inclusive upper bound, that is write i < right instead of i <= right and last - v instead of last - v - 1.

This has the advantage of making the base call qsort(v, 0, length) instead of qsort(v, 0, length - 1).

Context

StackExchange Code Review Q#26483, answer score: 2

Revisions (0)

No revisions yet.