Remove the option to copy the array from quick_sort, to be done in all other sorting functions, as that made the code needlessly complicated and prone to failure, still the tests need refactoring - as do the other sorting functions.
This commit is contained in:
parent
d772e8c9dc
commit
ca8c720759
|
@ -22,40 +22,21 @@ int standard_compare_quick_sort(const void* first, const void* second) {
|
|||
}
|
||||
|
||||
/* Note that if the comparison function is NULL, then the regular comparison operator is used <. */
|
||||
/* Note that if copy is set to 0, then the array is sorted in-place, otherwise a sorted copy
|
||||
* is what gets returned (after allocation), leaving the original intact. */
|
||||
/* 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. */
|
||||
/* Size specifies a single element's size. */
|
||||
/* 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, const int copy) {
|
||||
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)
|
||||
return NULL;
|
||||
|
||||
/* Local variables. */
|
||||
void **ptr;
|
||||
|
||||
/* Check if to allocate a copy. */
|
||||
if (copy) {
|
||||
/* Allocate the array. */
|
||||
ptr = (void**) malloc(length * sizeof(void*));
|
||||
|
||||
/* Check if the allocation failed. */
|
||||
if (ptr == NULL)
|
||||
return NULL;
|
||||
/* Copy the arrays' contents. */
|
||||
memcpy(ptr, array, length * sizeof(void*));
|
||||
} else
|
||||
ptr = array;
|
||||
|
||||
/* Rely on the standard qsort library function. */
|
||||
/* Sort according to ptr, and based on the employed comparison function. */
|
||||
if (comparison_function != NULL)
|
||||
qsort(ptr, length, size, standard_compare_quick_sort);
|
||||
qsort(array, length, size, standard_compare_quick_sort);
|
||||
else
|
||||
qsort(ptr, length, size, comparison_function);
|
||||
qsort(array, length, size, comparison_function);
|
||||
|
||||
/* Return the result. */
|
||||
return ptr;
|
||||
|
|
|
@ -57,10 +57,13 @@ int test_array_sort() {
|
|||
exit(ALLOCATION_ERROR_CODE);
|
||||
}
|
||||
|
||||
/* Attempt to copy the array. */
|
||||
result = (int**)
|
||||
|
||||
/* Set the timestamp. */
|
||||
start = clock();
|
||||
/* Run. */
|
||||
result = (int**) quick_sort(((void*)unsorted), comparison_function_int, ARRAY_LENGTH, sizeof(int), 1);
|
||||
result = (int**) quick_sort(((void*)result), comparison_function_int, ARRAY_LENGTH, sizeof(int));
|
||||
/* Set another timestamp. */
|
||||
end = clock();
|
||||
|
||||
|
@ -81,6 +84,11 @@ int test_array_sort() {
|
|||
for (i = 0; i < ARRAY_LENGTH; i++)
|
||||
assert(((int*)result)[i] == sorted[i]);
|
||||
|
||||
/* Free the result (since a copying call to quick_sort was used). */
|
||||
for (i = 0; i < ARRAY_LENGTH; i++)
|
||||
free(result[i]);
|
||||
free(result);
|
||||
|
||||
/* Ran successfully. */
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue