Skip to content

Commit

Permalink
updating bubble sort function
Browse files Browse the repository at this point in the history
  • Loading branch information
nkane committed Nov 17, 2018
1 parent e78d779 commit 50371e3
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 31 deletions.
43 changes: 20 additions & 23 deletions c-the-hard-way/exercises/33/liblcthw/src/lcthw/list_algos.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#include <lcthw/list_algos.h>
#include <lcthw/dbg.h>
#include <string.h>

void
inline void
ListNode_Swap(ListNode *n1, ListNode *n2)
{
void *temp = n1->value;
Expand All @@ -11,37 +10,35 @@ ListNode_Swap(ListNode *n1, ListNode *n2)
}

int
List_bubble_sort(List *list, List_compare list_compare_function)
List_Bubble_Sort(List *list, List_compare list_compare_function)
{
int result = -1;
if (list && list_compare_function)
int sorted = 1;
if (List_Count(list) <= 1)
{
return 0;
}
else
{
ListNode *node_one = list->first;
ListNode *node_two = node_one->next;
int i;
int j;
for (i = 0; i < list->count; i++)
{
node_one = list->first;
node_two = node_one->next;
for (j = 0; j < list->count - i - 1; j++)
do {
sorted = 1;
LIST_FOREACH(list, first, next, current)
{
// check if node_one value is greater than node_two value
if (list_compare_function(node_one, node_two) == 1)
if (current->next)
{
ListNode_Swap(node_one, node_two);
if (list_compare_function(current->value, current->next->value) > 0)
{
ListNode_Swap(current, current->next);
sorted = 0;
}
}
node_one = node_two;
node_two = node_one->next;
}
}
result = 0;
} while (!sorted);
}
return result;
return 0;
}

List *
List_merge_sort(List *list, List_compare list_compare_function)
List_Merge_Sort(List *list, List_compare list_compare_function)
{
if (list && list_compare_function)
{
Expand Down
4 changes: 2 additions & 2 deletions c-the-hard-way/exercises/33/liblcthw/src/lcthw/list_algos.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
typedef int (*List_compare)(const void *s1, const void *s2);

int
List_bubble_sort(List *list, List_compare list_compare_function);
List_Bubble_Sort(List *list, List_compare list_compare_function);

List *
List_merge_sort(List *list, List_compare list_compare_function);
List_Merge_Sort(List *list, List_compare list_compare_function);

#endif
Binary file not shown.
12 changes: 6 additions & 6 deletions c-the-hard-way/exercises/33/liblcthw/tests/list_algos_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,24 @@ test_bubble_sort()
List_Print(words, "Before Bubble Sort");

// should work on a list that needs sorting
int rc = List_bubble_sort(words, (List_compare) strcmp);
int rc = List_Bubble_Sort(words, (List_compare) strcmp);

List_Print(words, "After Bubble Sort");

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);
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(words);
rc = List_bubble_sort(words, (List_compare) strcmp);
mu_assert(rc = 0, "Bubble sort failed on empty list.");
rc = List_Bubble_Sort(words, (List_compare) strcmp);
mu_assert(rc == 0, "Bubble sort failed on empty list.");
mu_assert(is_sorted(words), "Words sould be sorted if empty.");

List_Destroy(words);
Expand All @@ -72,10 +72,10 @@ test_merge_sort()
List *words = create_words();

// should work on a list that needs sorting
List *res = List_merge_sort(res, (List_compare) strcmp);
List *res = List_Merge_Sort(res, (List_compare) strcmp);
mu_assert(is_sorted(res), "Words are not sorted after merge sort.");

List *res2 = List_merge_sort(res, (List_compare) strcmp);
List *res2 = List_Merge_Sort(res, (List_compare) strcmp);
mu_assert(is_sorted(res), "Should still be sorted after merge sort.");
List_Destroy(res2);
List_Destroy(res);
Expand Down
Binary file not shown.
34 changes: 34 additions & 0 deletions c-the-hard-way/exercises/33/liblcthw/tests/tests.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[DEBUG] tests/list_algos_tests.c:96: ----- RUNNING: ./tests/list_algos_tests
[DEBUG] tests/list_algos_tests.c:91:
----- test_bubble_sort
[INFO] (src/lcthw/list.c:140) Before Bubble Sort
[INFO] (src/lcthw/list.c:143) Node [0x1cc9018] - value: XXXX -- prev [(nil)] <- * -> next [0x1cc9028]
[INFO] (src/lcthw/list.c:143) Node [0x1cc9028] - value: 1234 -- prev [0x1cc9018] <- * -> next [0x1cc9038]
[INFO] (src/lcthw/list.c:143) Node [0x1cc9038] - value: abcd -- prev [0x1cc9028] <- * -> next [0x1cc9048]
[INFO] (src/lcthw/list.c:143) Node [0x1cc9048] - value: xjvef -- prev [0x1cc9038] <- * -> next [0x1cc9058]
[INFO] (src/lcthw/list.c:143) Node [0x1cc9058] - value: NDSS -- prev [0x1cc9048] <- * -> next [(nil)]
[INFO] (src/lcthw/list.c:140) After Bubble Sort
[INFO] (src/lcthw/list.c:143) Node [0x1cc9018] - value: 1234 -- prev [(nil)] <- * -> next [0x1cc9028]
[INFO] (src/lcthw/list.c:143) Node [0x1cc9028] - value: NDSS -- prev [0x1cc9018] <- * -> next [0x1cc9038]
[INFO] (src/lcthw/list.c:143) Node [0x1cc9038] - value: XXXX -- prev [0x1cc9028] <- * -> next [0x1cc9048]
[INFO] (src/lcthw/list.c:143) Node [0x1cc9048] - value: abcd -- prev [0x1cc9038] <- * -> next [0x1cc9058]
[INFO] (src/lcthw/list.c:143) Node [0x1cc9058] - value: xjvef -- prev [0x1cc9048] <- * -> next [(nil)]
[ERROR] (tests/list_algos_tests.c:61: errno: None) Bubble sort failed on empty list.
[DEBUG] tests/list_algos_tests.c:96: ----- RUNNING: ./tests/list_algos_tests
[DEBUG] tests/list_algos_tests.c:91:
----- test_bubble_sort
[INFO] (src/lcthw/list.c:140) Before Bubble Sort
[INFO] (src/lcthw/list.c:143) Node [0x10fd018] - value: XXXX -- prev [(nil)] <- * -> next [0x10fd028]
[INFO] (src/lcthw/list.c:143) Node [0x10fd028] - value: 1234 -- prev [0x10fd018] <- * -> next [0x10fd038]
[INFO] (src/lcthw/list.c:143) Node [0x10fd038] - value: abcd -- prev [0x10fd028] <- * -> next [0x10fd048]
[INFO] (src/lcthw/list.c:143) Node [0x10fd048] - value: xjvef -- prev [0x10fd038] <- * -> next [0x10fd058]
[INFO] (src/lcthw/list.c:143) Node [0x10fd058] - value: NDSS -- prev [0x10fd048] <- * -> next [(nil)]
[INFO] (src/lcthw/list.c:140) After Bubble Sort
[INFO] (src/lcthw/list.c:143) Node [0x10fd018] - value: 1234 -- prev [(nil)] <- * -> next [0x10fd028]
[INFO] (src/lcthw/list.c:143) Node [0x10fd028] - value: NDSS -- prev [0x10fd018] <- * -> next [0x10fd038]
[INFO] (src/lcthw/list.c:143) Node [0x10fd038] - value: XXXX -- prev [0x10fd028] <- * -> next [0x10fd048]
[INFO] (src/lcthw/list.c:143) Node [0x10fd048] - value: abcd -- prev [0x10fd038] <- * -> next [0x10fd058]
[INFO] (src/lcthw/list.c:143) Node [0x10fd058] - value: xjvef -- prev [0x10fd048] <- * -> next [(nil)]
[DEBUG] tests/list_algos_tests.c:92:
----- test_merge_sort
Segmentation fault

0 comments on commit 50371e3

Please sign in to comment.