Simplified and fixed the swap function, also fixed the bubble sort code.
This commit is contained in:
parent
233b303bff
commit
c723ad32bf
|
@ -22,29 +22,20 @@ void* bubble_sort(void* array, int (*comparison_function)(const void*, const voi
|
||||||
|
|
||||||
/* Local variables. */
|
/* Local variables. */
|
||||||
int i, j;
|
int i, j;
|
||||||
void *tmp;
|
|
||||||
|
|
||||||
/* Allocate the buffer. */
|
|
||||||
tmp = (void*) malloc(size);
|
|
||||||
if (tmp == NULL)
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
/* Sort according to array, and comparison function. */
|
/* Sort according to array, and comparison function. */
|
||||||
for (i = 0; i < length; i++)
|
for (i = 0; i < length; i++)
|
||||||
for (j = 0; j < length - 1; j++) {
|
for (j = 0; j < length - 1; j++) {
|
||||||
/* Check if to swap. */
|
/* Check if to swap. */
|
||||||
if (!comparison_function(array + j, array + j + 1)) {
|
if (!comparison_function(array + (j * size), array + ((j + 1) * size))) {
|
||||||
/* Swap. */
|
/* Swap. */
|
||||||
swap_void_ptr((array + j), (array + j + 1), tmp, size);
|
swap_void_ptr(array + (j * size), array + ((j + 1) * size), size);
|
||||||
/* tmp = array[j];
|
/*tmp = array[j];
|
||||||
* array[j] = array[j + 1];
|
array[j] = array[j + 1];
|
||||||
* array[j + 1] = tmp; */
|
array[j + 1] = tmp;*/
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Free the buffer. */
|
|
||||||
free(tmp);
|
|
||||||
|
|
||||||
/* Return the result. */
|
/* Return the result. */
|
||||||
return array;
|
return array;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,14 +1,15 @@
|
||||||
/* Function to swap void* objects. */
|
/* Function to swap void* objects. */
|
||||||
#include <stdlib.h>
|
|
||||||
#include "swap_void.h"
|
#include "swap_void.h"
|
||||||
|
|
||||||
/* This function swaps void* objects. */
|
/* This function swaps void* objects. */
|
||||||
/* Temp should be an allocated buffer, used to do the swap. */
|
|
||||||
/* Size should be > 0. */
|
/* Size should be > 0. */
|
||||||
/* Expect undefined behavior if conditions aren't met. */
|
/* Expect undefined behavior if conditions aren't met. */
|
||||||
extern inline void swap_void_ptr(void *first, void *second, void *temp, size_t size) {
|
extern inline void swap_void_ptr(void *first, void *second, size_t size) {
|
||||||
/* Swap. */
|
/* Swap. */
|
||||||
memcpy(temp, first, size);
|
char *a = (char*)first, *b = (char*)second, c;
|
||||||
memcpy(first, second, size);
|
while(size--) {
|
||||||
memcpy(second, temp, size);
|
c = a[size];
|
||||||
|
a[size] = b[size];
|
||||||
|
b[size] = c;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,6 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
/* This function is used to swap void* objects. */
|
/* This function is used to swap void* objects. */
|
||||||
extern inline void swap_void_ptr(void *first, void *second, void *temp, size_t size);
|
extern inline void swap_void_ptr(void *first, void *second, size_t size);
|
||||||
|
|
||||||
#endif //SWAP_VOID_H
|
#endif //SWAP_VOID_H
|
||||||
|
|
Loading…
Reference in New Issue