Tweaks after reviewing Zed's version
This commit is contained in:
parent
58a2ed31f4
commit
312c68e248
@ -1,9 +1,14 @@
|
|||||||
#include <lcthw/list_algos.h>
|
#include <lcthw/list_algos.h>
|
||||||
|
#include <lcthw/dbg.h>
|
||||||
|
|
||||||
int List_bubble_sort(List *list, List_compare cmp)
|
int List_bubble_sort(List *list, List_compare cmp)
|
||||||
{
|
{
|
||||||
List_validate(list);
|
List_validate(list);
|
||||||
|
|
||||||
|
if(List_count(list) <= 1) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
int swapped = 1;
|
int swapped = 1;
|
||||||
|
|
||||||
while(swapped == 1) {
|
while(swapped == 1) {
|
||||||
@ -24,7 +29,7 @@ int List_bubble_sort(List *list, List_compare cmp)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
List *_List_merge_sort_merge(List *left, List *right, List_compare cmp)
|
List *List_merge(List *left, List *right, List_compare cmp)
|
||||||
{
|
{
|
||||||
List *result = List_create();
|
List *result = List_create();
|
||||||
|
|
||||||
@ -51,7 +56,7 @@ List *_List_merge_sort_merge(List *left, List *right, List_compare cmp)
|
|||||||
List *List_merge_sort(List *list, List_compare cmp)
|
List *List_merge_sort(List *list, List_compare cmp)
|
||||||
{
|
{
|
||||||
if(List_count(list) <= 1) {
|
if(List_count(list) <= 1) {
|
||||||
return List_copy(list);;
|
return list;;
|
||||||
}
|
}
|
||||||
|
|
||||||
int i = 0;
|
int i = 0;
|
||||||
@ -67,13 +72,16 @@ List *List_merge_sort(List *list, List_compare cmp)
|
|||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
|
|
||||||
left = List_merge_sort(left, cmp);
|
List *sort_left = List_merge_sort(left, cmp);
|
||||||
right = List_merge_sort(right, cmp);
|
List *sort_right = List_merge_sort(right, cmp);
|
||||||
|
|
||||||
List *result = _List_merge_sort_merge(left, right, cmp);
|
|
||||||
|
|
||||||
|
if(sort_left != left) {
|
||||||
List_destroy(left);
|
List_destroy(left);
|
||||||
List_destroy(right);
|
}
|
||||||
|
|
||||||
return result;
|
if(sort_right != right) {
|
||||||
|
List_destroy(right);
|
||||||
|
}
|
||||||
|
|
||||||
|
return List_merge(sort_left, sort_right, cmp);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user