HeapSort

void HeapSort (void *arrayToSort, int numberOfElements, int elementSize, CompareFunction comparisonFunction);

Purpose

Sorts an array of elements in ascending order according to a comparison function provided by the user.

This function is similar to the C library qsort function except that its performance is guaranteed to be proportional to n*log n where n is the number of items to be sorted.

The qsort function uses the quicksort algorithm with median of 3 partitioning. The performance of this algorithm is n*log n for almost all initial orderings, including the case where the array is already sorted. However, there are a very few orderings for which the performance of quicksort degenerates towards being proportional to n*n.

In practice, you will almost never encounter one of these orderings. This makes the C library qsort function a better choice since it is about three times faster than HeapSort.

Only use HeapSort if sorting performance proportional to n*log n must be absolutely guaranteed.

Parameters

Input
Name Type Description
arrayToSort void * The array of items that HeapSort will sort.
numberOfElements integer Pass the number of items in the array.
elementSize integer Pass the item size (in bytes) for the items in the array.
comparisonFunction CompareFunction Pass a comparison function that HeapSort should use to compare the items in the array.

The comparison function should have the following prototype:

int CVICALLBACK CompareFunction(void *item1, void *item2);


The comparison function should return a negative number if item1 is less than item2, it should return 0 if item1 is equal to item2, and it should return a positive number if item1 is greater than item2.

This instrument driver provides several commonly useful comparison functions:

ShortCompare
IntCompare
FloatCompare
DoubleCompare
CStringCompare
CStringNoCaseCompare

Return Value

None.