Admin Production ni-theme
Current Publication

InsertionSort

LabWindows/CVI

InsertionSort

void InsertionSort (void *arrayToSort, size_t numberOfElements, size_t 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 it uses a plain n–squared sorting algorithm. Its only advantage is that it is a stable sort.

A stable sort can be used multiple times on the same data set with different keys. For example, if InsertionSort is called to sort an array of names by first name and then called again to sort the same array by last name, then the names will be sorted by last name and sorted by first name where the last names are the same (like a phone book). The Quick Sort algorithm does not have this property. However, a comparison function could be written to compare based on last names with ties broken by comparing on first names. This comparison function could be passed to qsort or HeapSort to achieve the same results with greater efficiency.

While this function can be fast for small numbers of items, its n-squared property makes it unsuitable for very large numbers of items. For large numbers of items, you should use qsort or another n * log n sort.

Parameters

Input
Name Type Description
arrayToSort void * The array of items that InsertionSort will sort.
numberOfElements size_t Pass the number of items in the array.
elementSize size_t Pass the item size (in bytes) for the items in the array.
comparisonFunction CompareFunction Pass a comparison function that InsertionSort 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.

Additional Information

Library: Programmer's Toolbox

Include file: toolbox\toolbox.h

LabWindows/CVI compatibility: LabWindows/CVI 4.0 and later