I notice that <stdlib.h>
only includes qsort
which is not stable. Therefore I cannot sort a composite data structure along different columns.
Is there any standard (POSIX/C11) and stable sorting algorithms in C?
I notice that <stdlib.h>
only includes qsort
which is not stable. Therefore I cannot sort a composite data structure along different columns.
Is there any standard (POSIX/C11) and stable sorting algorithms in C?
Standard C library provides qsort() that can be used for sorting an array. As the name suggests, the function uses QuickSort algorithm to sort the given array. Following is prototype of qsort()
void qsort (void* base, size_t num, size_t size,
int (*comparator)(const void*,const void*));
The key point about qsort() is comparator function comparator. The comparator function takes two arguments and contains logic to decide their relative order in sorted output. The idea is to provide flexibility so that qsort() can be used for any type (including user defined types) and can be used to obtain any desired order (increasing, decreasing or any other).
The comparator function takes two pointers as arguments (both type-casted to const void*) and defines the order of the elements by returning (in a stable and transitive manner).
Still if you get any doubt fell free to ask.