20 unsigned char pivot,
size_t left_size)
24 for (i = 0; i < left_size; i++)
25 if (array[i] >= pivot)
37swap (
unsigned char *a,
unsigned char *b)
48partition (
unsigned char *array,
size_t size,
int pivot)
50 size_t left_size = size;
51 unsigned char *first = array;
52 unsigned char *last = first + left_size;
65 else if (*first >= pivot)
83 else if (*last < pivot)
104 for (i = 1; i < size; i++)
120 unsigned char *left_half =
buf;
122 unsigned char *right_half = left_half + left_size;
123 size_t right_size = size - left_size;
125 if (left_size <= right_size)
static char buf[BUF_SIZE]
#define ASSERT(CONDITION)
This is outside the header guard so that debug.h may be included multiple times with different settin...
static void swap(unsigned char *a, unsigned char *b)
Swaps the bytes at *A and *B.
static unsigned char pick_pivot(unsigned char *buf, size_t size)
Picks a pivot for the quicksort from the SIZE bytes in BUF.
void qsort_bytes(unsigned char *buf, size_t size)
Sorts the SIZE bytes in BUF into nondecreasing order, using the quick-sort algorithm.
static bool is_partitioned(const unsigned char *array, size_t size, unsigned char pivot, size_t left_size)
Checks whether the SIZE bytes in ARRAY are divided into an initial LEFT_SIZE elements all less than P...
static size_t partition(unsigned char *array, size_t size, int pivot)
Partitions ARRAY in-place in an initial run of bytes all less than PIVOT, followed by a run of bytes ...
static bool is_sorted(const unsigned char *buf, size_t size)
Returns true if the SIZE bytes in BUF are in nondecreasing order, false otherwise.
unsigned long random_ulong(void)
Returns a pseudo-random unsigned long.