Quick Sort tested and working, copy the changes to other sorting functions and their test code in the next commit.
This commit is contained in:
parent
ca8c720759
commit
4e8763376b
|
@ -26,7 +26,7 @@ int standard_compare_quick_sort(const void* first, const void* second) {
|
|||
* 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) {
|
||||
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;
|
||||
|
@ -39,5 +39,5 @@ void** quick_sort(void** array, int (*comparison_function)(const void*, const vo
|
|||
qsort(array, length, size, comparison_function);
|
||||
|
||||
/* Return the result. */
|
||||
return ptr;
|
||||
return array;
|
||||
}
|
||||
|
|
|
@ -5,5 +5,5 @@
|
|||
/* 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, const int copy);
|
||||
void* quick_sort(void* array, int (*comparison_function)(const void*, const void*), const size_t length, const size_t size);
|
||||
#endif /* QUICK_SORT_H */
|
||||
|
|
|
@ -47,7 +47,7 @@ int test_array_sort() {
|
|||
/* Local variables. */
|
||||
clock_t start, end;
|
||||
function_run_time *runtime;
|
||||
int **result;
|
||||
int *result;
|
||||
int i;
|
||||
|
||||
/* Attempt to allocate the struct. */
|
||||
|
@ -58,12 +58,19 @@ int test_array_sort() {
|
|||
}
|
||||
|
||||
/* Attempt to copy the array. */
|
||||
result = (int**)
|
||||
result = (int*) malloc (ARRAY_LENGTH * sizeof(int));
|
||||
if (result == NULL) {
|
||||
printf(TESTING_ALLOCATION_ERROR);
|
||||
free(runtime);
|
||||
exit(ALLOCATION_ERROR_CODE);
|
||||
}
|
||||
for (int i = 0; i < ARRAY_LEGNTH; i++)
|
||||
result[i] = unsorted[i];
|
||||
|
||||
/* Set the timestamp. */
|
||||
start = clock();
|
||||
/* Run. */
|
||||
result = (int**) quick_sort(((void*)result), comparison_function_int, ARRAY_LENGTH, sizeof(int));
|
||||
result = (int*) quick_sort(((void*)result), comparison_function_int, ARRAY_LENGTH, sizeof(int));
|
||||
/* Set another timestamp. */
|
||||
end = clock();
|
||||
|
||||
|
@ -82,11 +89,9 @@ int test_array_sort() {
|
|||
|
||||
/* Check if the array is sorted. */
|
||||
for (i = 0; i < ARRAY_LENGTH; i++)
|
||||
assert(((int*)result)[i] == sorted[i]);
|
||||
assert(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 the result. */
|
||||
free(result);
|
||||
|
||||
/* Ran successfully. */
|
||||
|
|
Loading…
Reference in New Issue