ex33 with my impl for bubble/merge sorts
This commit is contained in:
@@ -210,3 +210,35 @@ void List_join(List *list, List *b)
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
void List_swap(ListNode *a, ListNode *b)
|
||||
{
|
||||
ListNode *tmp = a->next;
|
||||
a->next = b->next;
|
||||
b->next = tmp;
|
||||
tmp = a->prev;
|
||||
a->prev = b->prev;
|
||||
b->prev = tmp;
|
||||
}
|
||||
|
||||
|
||||
void List_dump(List *list)
|
||||
{
|
||||
List_validate(list);
|
||||
|
||||
int i = 0;
|
||||
int j = 0;
|
||||
|
||||
LIST_FOREACH(list, first, next, cur) {
|
||||
if(i > 0) {
|
||||
for(j = 0; j < (i*4); j++) {
|
||||
printf(" ");
|
||||
}
|
||||
printf("`");
|
||||
}
|
||||
printf("-> [%d] ListNode .value = %p (%s)\n", i, cur->value, cur->value);
|
||||
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
@@ -38,12 +38,14 @@ void *List_remove(List *list, ListNode *node);
|
||||
List *List_copy(List *list);
|
||||
int List_split(List *list, void *split, List *a, List *b);
|
||||
void List_join(List *list, List *b);
|
||||
void List_swap(ListNode *a, ListNode *b);
|
||||
void List_dump(List *list);
|
||||
|
||||
#define List_validate(A) (assert(A != NULL && List_count(A) > -1 &&\
|
||||
(List_count(A) > 0 && List_first(A) != NULL) && "invalid *List"))
|
||||
|
||||
#define LIST_FOREACH(L, S, M, V) ListNode *_node = NULL;\
|
||||
ListNode *V = NULL;\
|
||||
for(V = _node = L->S; _node != NULL; V = _node = _node->M)
|
||||
#define LIST_FOREACH(L, F, N, C) ListNode *_node = NULL;\
|
||||
ListNode *C = NULL;\
|
||||
for(C = _node = L->F; _node != NULL; C = _node = _node->N)
|
||||
|
||||
#endif
|
||||
|
79
lcthw-remnants-2/liblcthw/src/lcthw/list_algos.c
Normal file
79
lcthw-remnants-2/liblcthw/src/lcthw/list_algos.c
Normal file
@@ -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;
|
||||
}
|
12
lcthw-remnants-2/liblcthw/src/lcthw/list_algos.h
Normal file
12
lcthw-remnants-2/liblcthw/src/lcthw/list_algos.h
Normal file
@@ -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
|
Reference in New Issue
Block a user