Simplified quick sort implementation.
This commit is contained in:
parent
d941802534
commit
42383ceec6
|
@ -2,41 +2,24 @@
|
|||
/* Include guards + include statements. */
|
||||
#ifndef QUCIK_SORT_C
|
||||
#define QUICK_SORT_C
|
||||
#include <string.h>
|
||||
#include "quicksort.h"
|
||||
#endif /* QUICK_SORT_C */
|
||||
|
||||
/* Function implementations. */
|
||||
|
||||
/* This is the standard (<) comparison function. */
|
||||
int standard_compare_quick_sort(const void* first, const void* second) {
|
||||
/* Sanity check. */
|
||||
if (first == NULL && second != NULL)
|
||||
return 1;
|
||||
else if (first != NULL && second == NULL)
|
||||
return 0;
|
||||
else if (first == NULL && second == NULL)
|
||||
return 0;
|
||||
else
|
||||
return *((int*)first) <= *((int*)second); /* THIS IS WRONG AND SHOULD BE FIXED! */
|
||||
}
|
||||
|
||||
/* Note that if the comparison function is NULL, then the regular comparison operator is used <. */
|
||||
/* 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)
|
||||
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. */
|
||||
if (comparison_function != NULL)
|
||||
qsort(array, length, size, standard_compare_quick_sort);
|
||||
else
|
||||
qsort(array, length, size, comparison_function);
|
||||
qsort(array, length, size, comparison_function);
|
||||
|
||||
/* Return the result. */
|
||||
return array;
|
||||
|
|
|
@ -1,9 +1,7 @@
|
|||
/* This header defines the function for quick sort. */
|
||||
#ifndef QUICK_SORT_H
|
||||
#define QUICK_SORT_H
|
||||
#include <aio.h>
|
||||
/* This is the standard (<) comparison function. */
|
||||
int standard_compare_quick_sort(const void* first, const void* second);
|
||||
|
||||
/* The quicksort function's definition. */
|
||||
void* quick_sort(void* array, int (*comparison_function)(const void*, const void*), const size_t length, const size_t size);
|
||||
#endif /* QUICK_SORT_H */
|
||||
|
|
Loading…
Reference in New Issue