Improved bubble sort code, still need to figure out how to get the linker to link it against all the support code.
This commit is contained in:
parent
4116a6cfbf
commit
06c58d8995
|
@ -4,6 +4,7 @@
|
|||
#define BUBBLE_SORT_C
|
||||
#include <string.h>
|
||||
#include "bubble_sort.h"
|
||||
#include "../utils/swap_void.h"
|
||||
#endif /* BUBBLE_SORT_C */
|
||||
|
||||
/* Function implementation. */
|
||||
|
@ -11,28 +12,38 @@
|
|||
/* 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 should match a single object's size, and should be > 0. */
|
||||
/* The function returns a NULL pointer on error, caller must check! */
|
||||
void* bubble_sort(void* array, int (*comparison_function)(const void*, const void*), const int length) {
|
||||
void* bubble_sort(void* array, int (*comparison_function)(const void*, const void*), const int length, size_t size) {
|
||||
/* Sanity check. */
|
||||
if (array == NULL || comparison_function == NULL)
|
||||
if (array == NULL || comparison_function == NULL || size <= 0)
|
||||
return NULL;
|
||||
|
||||
/* Local variables. */
|
||||
int i, j;
|
||||
void *tmp;
|
||||
|
||||
/* Allocate the buffer. */
|
||||
tmp = (void*) malloc(size);
|
||||
if (tmp == NULL)
|
||||
return NULL;
|
||||
|
||||
/* Sort according to array, and comparison function. */
|
||||
for (i = 0; i < length; i++)
|
||||
for (j = 0; j < length - 1; j++) {
|
||||
/* Check if to swap. */
|
||||
if (!comparison_function(array + j, array + j + 1)) {
|
||||
/* Swap. */
|
||||
tmp = array[j];
|
||||
array[j] = array[j + 1];
|
||||
array[j + 1] = tmp;
|
||||
swap_void_ptr((array + j), (array + j + 1), tmp, size);
|
||||
/* tmp = array[j];
|
||||
* array[j] = array[j + 1];
|
||||
* array[j + 1] = tmp; */
|
||||
}
|
||||
}
|
||||
|
||||
/* Free the buffer. */
|
||||
free(tmp);
|
||||
|
||||
/* Return the result. */
|
||||
return array;
|
||||
}
|
||||
|
|
|
@ -5,5 +5,5 @@
|
|||
#include <stdlib.h>
|
||||
|
||||
/* Function definitions. */
|
||||
void** bubble_sort(void** array, int (*comparison_function)(const void*, const void*), const int length);
|
||||
void* bubble_sort(void* array, int (*comparison_function)(const void*, const void*), const int length, size_t size);
|
||||
#endif /* BUBBLE_SORT_H */
|
||||
|
|
|
@ -1,10 +1,17 @@
|
|||
/* Include statements. */
|
||||
#include <stdio.h>
|
||||
/*#include <assert.h>*/
|
||||
#include <assert.h>
|
||||
#include <time.h>
|
||||
#include <stdlib.h>
|
||||
#include "../sorting_algorithms/bubble_sort/bubble_sort.c"
|
||||
/* Include the struct to store the measurements and function name. */
|
||||
#include "../utils/time_function.h"
|
||||
/* For easier debugging. */
|
||||
#ifdef DEBUG
|
||||
#include "../utils/array_print.h"
|
||||
#else
|
||||
#define print_integer_array(result, length)
|
||||
#endif
|
||||
|
||||
/* Define constants. */
|
||||
#define PRINT_INFO_TESTING_START "Starting tests.\n"
|
||||
|
@ -56,23 +63,25 @@ int test_array_sort() {
|
|||
exit(ALLOCATION_ERROR_CODE);
|
||||
}
|
||||
|
||||
/* Allocate the array. */
|
||||
array = (int*) malloc(ARRAY_LENGTH * sizeof(int));
|
||||
/* Attempt to copy the array. */
|
||||
result = (int*) malloc(ARRAY_LENGTH * sizeof(int));
|
||||
/* Check for allocation failure. */
|
||||
if (array == NULL) {
|
||||
free(runtime);
|
||||
if (result == NULL) {
|
||||
printf(TESTING_ALLOCATION_ERROR);
|
||||
free(runtime);
|
||||
exit(ALLOCATION_ERROR_CODE);
|
||||
}
|
||||
|
||||
/* Copy the array. */
|
||||
/* Copy. */
|
||||
for (i = 0; i < ARRAY_LENGTH; i++)
|
||||
result[i] = unsorted[i];
|
||||
|
||||
/* For debugging purposes. */
|
||||
print_integer_array(result, ARRAY_LENGTH);
|
||||
|
||||
/* Set the timestamp. */
|
||||
start = clock();
|
||||
/* Run. */
|
||||
result = (int*) bubble_sort(((void*)result), comparison_function_int, ARRAY_LENGTH);
|
||||
result = (int*) bubble_sort(((void*)result), comparison_function_int, ARRAY_LENGTH, sizeof(int));
|
||||
/* Set another timestamp. */
|
||||
end = clock();
|
||||
|
||||
|
@ -84,15 +93,20 @@ int test_array_sort() {
|
|||
runtime->time = end - start;
|
||||
|
||||
/* Print the results. */
|
||||
printf(RUNNING_TIME, runtime->time);
|
||||
printf(RUNNING_TIME, ((double)((runtime->time)/CLOCKS_PER_SEC)));
|
||||
|
||||
/* Free the struct. */
|
||||
free(runtime);
|
||||
|
||||
/* For debugging purposes. */
|
||||
print_integer_array(result, ARRAY_LENGTH);
|
||||
|
||||
/* Check if the array is sorted. */
|
||||
for (i = 0; i < ARRAY_LENGTH; i++)
|
||||
if (result[i] != sorted[i])
|
||||
return 1;
|
||||
assert(result[i] == sorted[i]);
|
||||
|
||||
/* Free the result. */
|
||||
free(result);
|
||||
|
||||
/* Ran successfully. */
|
||||
return 0;
|
||||
|
|
|
@ -7,8 +7,11 @@ binary_search_tree.o: ../trees/binary_search_tree/binary_search_tree.c ../trees/
|
|||
bst_tests.o: bst_tests.c
|
||||
gcc -g -c bst_tests.c
|
||||
|
||||
bubble_sort_tests.out: bubble_sort_tests.c
|
||||
gcc -g -fsanitize=address -fsanitize=leak -fsanitize=undefined bubble_sort_tests.c -o bubble_sort_tests.out
|
||||
bubble_sort_tests.out: swap_void.o array_print.o bubble_sort_tests.o
|
||||
gcc -g -fsanitize=address -fsanitize=leak -fsanitize=undefined swap_void.o array_print.o bubble_sort_tests.o -o bubble_sort_tests.out -lm
|
||||
|
||||
bubble_sort_tests.o: bubble_sort_tests.c
|
||||
gcc -g -I ../utils -D DEBUG -c bubble_sort_tests.c
|
||||
|
||||
double_linked_list_tests.out: double_linked_list_tests.c
|
||||
gcc -g -fsanitize=address -fsanitize=leak -fsanitize=undefined double_linked_list_tests.c -o double_linked_list_tests.out
|
||||
|
@ -18,11 +21,14 @@ quick_sort_tests.out: array_print.o quick_sort_tests.o
|
|||
gcc -g -fsanitize=address -fsanitize=leak -fsanitize=undefined array_print.o quick_sort_tests.o -o quick_sort_tests.out -lm
|
||||
|
||||
quick_sort_tests.o: quick_sort_tests.c
|
||||
gcc -g -c quick_sort_tests.c
|
||||
gcc -g -D DEBUG -c quick_sort_tests.c
|
||||
|
||||
array_print.o: ../utils/array_print.c
|
||||
gcc -g -c ../utils/array_print.c
|
||||
|
||||
swap_void.o: ../utils/swap_void.c
|
||||
gcc -g -c ../utils/swap_void.c
|
||||
|
||||
single_linked_list_tests.out: single_linked_list_tests.c
|
||||
gcc -g -fsanitize=address -fsanitize=leak -fsanitize=undefined single_linked_list_tests.c -o single_linked_list_tests.out
|
||||
|
||||
|
|
Loading…
Reference in New Issue