parent
c4b92572bf
commit
80b8ecc4fa
@ -0,0 +1,79 @@
|
||||
#include <lcthw/list_algos.h>
|
||||
|
||||
int List_bubble_sort(List *list, List_compare cmp)
|
||||
{
|
||||
List_validate(list);
|
||||
|
||||
int swapped = 1;
|
||||
|
||||
while(swapped == 1) {
|
||||
swapped = 0;
|
||||
|
||||
LIST_FOREACH(list, first, next, cur) {
|
||||
if(cur->next == NULL) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if(cmp(cur->value, cur->next->value) > 0) {
|
||||
List_swap(cur, cur->next);
|
||||
swapped = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
List *_List_merge_sort_merge(List *left, List *right, List_compare cmp)
|
||||
{
|
||||
List *result = List_create();
|
||||
|
||||
while(List_count(left) > 0 && List_count(right) > 0) {
|
||||
if(cmp(List_first(left), List_first(right)) <= 0) {
|
||||
List_push(result, List_shift(left));
|
||||
continue;
|
||||
}
|
||||
|
||||
List_push(result, List_shift(right));
|
||||
}
|
||||
|
||||
while(List_count(left) > 0) {
|
||||
List_push(result, List_shift(left));
|
||||
}
|
||||
|
||||
while(List_count(right) > 0) {
|
||||
List_push(result, List_shift(right));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
List *List_merge_sort(List *list, List_compare cmp)
|
||||
{
|
||||
if(List_count(list) <= 1) {
|
||||
return List_copy(list);;
|
||||
}
|
||||
|
||||
int i = 0;
|
||||
List *left = List_create();
|
||||
List *right = List_create();
|
||||
|
||||
LIST_FOREACH(list, first, next, cur) {
|
||||
if(i % 2 == 0) {
|
||||
List_push(right, cur->value);
|
||||
} else {
|
||||
List_push(left, cur->value);
|
||||
}
|
||||
i++;
|
||||
}
|
||||
|
||||
left = List_merge_sort(left, cmp);
|
||||
right = List_merge_sort(right, cmp);
|
||||
|
||||
List *result = _List_merge_sort_merge(left, right, cmp);
|
||||
|
||||
List_destroy(left);
|
||||
List_destroy(right);
|
||||
|
||||
return result;
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
#ifndef lcthw_List_algos_h
|
||||
#define lcthw_List_algos_h
|
||||
|
||||
#include <lcthw/list.h>
|
||||
|
||||
typedef int (*List_compare)(const void *a, const void *b);
|
||||
|
||||
int List_bubble_sort(List *list, List_compare cmp);
|
||||
|
||||
List *List_merge_sort(List *list, List_compare cmp);
|
||||
|
||||
#endif
|
@ -0,0 +1,97 @@
|
||||
#undef NDEBUG
|
||||
#include "../minunit.h"
|
||||
#include <lcthw/list_algos.h>
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
|
||||
char *values[] = {"XXXX", "1234", "abcd", "xjvef", "NDSS"};
|
||||
#define NUM_VALUES 5
|
||||
|
||||
List *create_words()
|
||||
{
|
||||
int i = 0;
|
||||
List *words = List_create();
|
||||
|
||||
for(i = 0; i < NUM_VALUES; i++) {
|
||||
List_push(words, values[i]);
|
||||
}
|
||||
|
||||
return words;
|
||||
}
|
||||
|
||||
int is_sorted(List *words)
|
||||
{
|
||||
LIST_FOREACH(words, first, next, cur) {
|
||||
if(cur->next && strcmp(cur->value, cur->next->value) > 0) {
|
||||
debug("%s %s", (char *)cur->value, (char *)cur->next->value);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
char *test_bubble_sort()
|
||||
{
|
||||
List *words = create_words();
|
||||
|
||||
// should work on a list that needs sorting
|
||||
int rc = List_bubble_sort(words, (List_compare)strcmp);
|
||||
mu_assert(rc == 0, "Bubble sort failed.");
|
||||
mu_assert(is_sorted(words), "Words are not sorted after bubble sort.");
|
||||
|
||||
// should work on an already sorted list
|
||||
rc = List_bubble_sort(words, (List_compare)strcmp);
|
||||
mu_assert(rc == 0, "Bubble sort of already sorted failed.");
|
||||
mu_assert(is_sorted(words), "Words should be sorted if already bubble sorted.");
|
||||
|
||||
List_destroy(words);
|
||||
|
||||
// should work on an empty list
|
||||
words = List_create();
|
||||
rc = List_bubble_sort(words, (List_compare)strcmp);
|
||||
mu_assert(rc == 0, "Bubble sort failed on empty list.");
|
||||
mu_assert(is_sorted(words), "Words should be sorted if empty.");
|
||||
|
||||
List_destroy(words);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char *test_merge_sort()
|
||||
{
|
||||
List *words = create_words();
|
||||
|
||||
// should work on a list that needs sorting
|
||||
List *res = List_merge_sort(words, (List_compare)strcmp);
|
||||
mu_assert(List_count(res) == List_count(words), "Sorted list has different count.");
|
||||
mu_assert(is_sorted(res), "Words are not sorted after merge sort.");
|
||||
|
||||
List *res2 = List_merge_sort(res, (List_compare)strcmp);
|
||||
mu_assert(List_count(res2) == List_count(res), "Sorted list has different count.");
|
||||
mu_assert(is_sorted(res), "Should still be sorted after merge sort.");
|
||||
|
||||
List_destroy(res2);
|
||||
List_destroy(res);
|
||||
|
||||
List_destroy(words);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
char *all_tests()
|
||||
{
|
||||
mu_suite_start();
|
||||
|
||||
int i = 0;
|
||||
for(i = 0; i < NUM_VALUES; i++) {
|
||||
printf("%s -> %p\n", values[i], values[i]);
|
||||
}
|
||||
|
||||
mu_run_test(test_bubble_sort);
|
||||
mu_run_test(test_merge_sort);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
RUN_TESTS(all_tests);
|
Loading…
Reference in new issue