27 lines
1.0 KiB
C
27 lines
1.0 KiB
C
/* This file has the implementation of the function for quick sort. */
|
|
/* Include guards + include statements. */
|
|
#ifndef QUCIK_SORT_C
|
|
#define QUICK_SORT_C
|
|
#include "quicksort.h"
|
|
#endif /* QUICK_SORT_C */
|
|
|
|
/* Function implementations. */
|
|
|
|
/* Note that if the comparison function is NULL, then the function will quit! */
|
|
/* Note that the comparison function should return 1 if the second argument is larger than or
|
|
* equal to the first, otherwise zero. */
|
|
/* The array's length should be passed in the length argument. */
|
|
/* The function returns a NULL pointer on error, caller must check! */
|
|
void* quick_sort(void* array, int (*comparison_function)(const void*, const void*), const size_t length, const size_t size) {
|
|
/* Sanity check. */
|
|
if (array == NULL || comparison_function == NULL)
|
|
return NULL;
|
|
|
|
/* Rely on the standard qsort library function. */
|
|
/* Sort according to ptr, and based on the employed comparison function. */
|
|
qsort(array, length, size, comparison_function);
|
|
|
|
/* Return the result. */
|
|
return array;
|
|
}
|