Start simplifying bubble sort implementation.
This commit is contained in:
parent
7991eb8015
commit
d941802534
|
@ -7,59 +7,32 @@
|
|||
#endif /* BUBBLE_SORT_C */
|
||||
|
||||
/* Function implementation. */
|
||||
/* 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 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** bubble_sort(void** array, int (*comparison_function)(const void*, const void*), const int length, const int copy) {
|
||||
void* bubble_sort(void* array, int (*comparison_function)(const void*, const void*), const int length) {
|
||||
/* Sanity check. */
|
||||
if (array == NULL)
|
||||
if (array == NULL || comparison_function == NULL)
|
||||
return NULL;
|
||||
|
||||
/* Local variables. */
|
||||
int i, j;
|
||||
void **ptr, *tmp;
|
||||
void *tmp;
|
||||
|
||||
/* 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;
|
||||
|
||||
/* Sort according to ptr, and based on the employed comparison function. */
|
||||
if (comparison_function != NULL)
|
||||
for (i = 0; i < length; i++)
|
||||
for (j = 0; j < length - 1; j++) {
|
||||
/* Check if to swap. */
|
||||
if (!comparison_function(ptr + j, ptr + j + 1)) {
|
||||
/* Swap. */
|
||||
tmp = ptr[j];
|
||||
ptr[j] = ptr[j + 1];
|
||||
ptr[j + 1] = tmp;
|
||||
}
|
||||
}
|
||||
else
|
||||
for (i = 0; i < length; i++)
|
||||
for (j = 0; j < length; j++) {
|
||||
/* Check if to swap. */
|
||||
if (ptr[j] > ptr[j + 1]) {
|
||||
/* Swap. */
|
||||
tmp = ptr[j];
|
||||
ptr[j] = ptr[j + 1];
|
||||
ptr[j + 1] = tmp;
|
||||
}
|
||||
/* 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;
|
||||
}
|
||||
}
|
||||
|
||||
/* Return the result. */
|
||||
return ptr;
|
||||
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, const int copy);
|
||||
void** bubble_sort(void** array, int (*comparison_function)(const void*, const void*), const int length);
|
||||
#endif /* BUBBLE_SORT_H */
|
||||
|
|
Loading…
Reference in New Issue