HGPA

sorting

Sorting is a fundamental problem in computer science. Each method of sorting has trade-offs in time and space complexity. Sorting is often the first step in solving more complex problems.

Sorting Functions

Most of the time we don't care about any of this theoretical stuff. We just want the damn thing sorted! Most languages include some sort of sorting, either through their standard libraries, or as method on a fundamental data type.

C has the qsort sorting function in its standard library. Despite its name it is not necessarily quick sort. The implementation is vendor specific. qsort requires 4 parameters:

#include <stdlib.h>

/* basic integer comparator function */
int cmp(const void* elem1, const void* elem2) {
    int *x = (int *) elem1;
    int *y = (int *) elem2;
    return *x - *y;
}

qsort(array, array_length, sizeof(int), cmp);

**

Sorting Implementations

Methods of sorting include:

Implementations of sorting algorithms can be tricky. Why?

Filename: sorting.md (Edit)
Modified: 2017-02-05 (d6d58f0e809416dddddad084d0d583c17f105a15)
Created: 2016-12-19 (d3364489238c872ad65dd9b075a171f5cef98a99)