From e2add348d94b997680c21310593190701fe36f77 Mon Sep 17 00:00:00 2001 From: northernSage Date: Wed, 17 Feb 2021 07:08:51 -0300 Subject: [PATCH 01/10] apply snake case --- sorting/bubble_sort_2.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/sorting/bubble_sort_2.c b/sorting/bubble_sort_2.c index 9e3c8000..c26a0ca8 100644 --- a/sorting/bubble_sort_2.c +++ b/sorting/bubble_sort_2.c @@ -6,7 +6,7 @@ int main() { - int i, arraySort[MAX] = {0}, isSort = FALSE, changePlace; + int i, array_sort[MAX] = {0}, is_sorted = FALSE, change_place; /* For example Insertion random values in array to test @@ -14,23 +14,23 @@ int main() for (i = 0; i < MAX; i++) { - arraySort[i] = rand() % 101; + array_sort[i] = rand() % 101; } /* Algorithm of bubble methods */ - while (isSort) + while (is_sorted) { - isSort = FALSE; + is_sorted = FALSE; for (i = 0; i < MAX - 1; i++) { - if (arraySort[i] > arraySort[i + 1]) + if (array_sort[i] > array_sort[i + 1]) { - changePlace = arraySort[i]; - arraySort[i] = arraySort[i + 1]; - arraySort[i + 1] = changePlace; - isSort = TRUE; + change_place = array_sort[i]; + array_sort[i] = array_sort[i + 1]; + array_sort[i + 1] = change_place; + is_sorted = TRUE; } } } @@ -39,7 +39,7 @@ int main() for (i = 0; i < MAX; i++) { - printf("%d\n", arraySort[i]); + printf("%d\n", array_sort[i]); } return EXIT_SUCCESS; From 6b0f57d372c13c70b39c394155733436a6c7dccd Mon Sep 17 00:00:00 2001 From: northernSage Date: Wed, 17 Feb 2021 07:11:11 -0300 Subject: [PATCH 02/10] use stdbool.h instead of manually defined macros --- sorting/bubble_sort_2.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/sorting/bubble_sort_2.c b/sorting/bubble_sort_2.c index c26a0ca8..205de9e0 100644 --- a/sorting/bubble_sort_2.c +++ b/sorting/bubble_sort_2.c @@ -1,12 +1,12 @@ #include #include #define MAX 20 -#define TRUE 1 -#define FALSE 0 + +#include int main() { - int i, array_sort[MAX] = {0}, is_sorted = FALSE, change_place; + int i, array_sort[MAX] = {0}, is_sorted = false, change_place; /* For example Insertion random values in array to test @@ -21,7 +21,7 @@ int main() while (is_sorted) { - is_sorted = FALSE; + is_sorted = false; for (i = 0; i < MAX - 1; i++) { @@ -30,7 +30,7 @@ int main() change_place = array_sort[i]; array_sort[i] = array_sort[i + 1]; array_sort[i + 1] = change_place; - is_sorted = TRUE; + is_sorted = true; } } } From 3b647a617a663c99fbaf4373668a9f34b6129008 Mon Sep 17 00:00:00 2001 From: northernSage Date: Wed, 17 Feb 2021 07:13:29 -0300 Subject: [PATCH 03/10] move bubblesort implementation to separate function --- sorting/bubble_sort_2.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/sorting/bubble_sort_2.c b/sorting/bubble_sort_2.c index 205de9e0..787e7972 100644 --- a/sorting/bubble_sort_2.c +++ b/sorting/bubble_sort_2.c @@ -4,6 +4,40 @@ #include +/** + * Bubble sort implementation + * @param array_sort the array to be sorted + */ +void bubblesort(int* array_sort) +{ + bool is_sorted = false; + + /* keep iterating over entire array + * and swaping elements out of order + * until it is sorted */ + while (!is_sorted) + { + is_sorted = true; + + /* iterate over all elements */ + for (int i = 0; i < MAX - 1; i++) + { + /* check if adjacent elements are out of order */ + if (array_sort[i] > array_sort[i + 1]) + { + /* swap elements */ + int change_place = array_sort[i]; + array_sort[i] = array_sort[i + 1]; + array_sort[i + 1] = change_place; + /* elements out of order were found + * so we reset the flag to keep ordering + * until no swap operations are executed */ + is_sorted = false; + } + } + } +} + int main() { int i, array_sort[MAX] = {0}, is_sorted = false, change_place; From 9ba58f4571d24e6b067a6dcf2ecd0b6793562273 Mon Sep 17 00:00:00 2001 From: northernSage Date: Wed, 17 Feb 2021 07:14:34 -0300 Subject: [PATCH 04/10] add separate test function --- sorting/bubble_sort_2.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/sorting/bubble_sort_2.c b/sorting/bubble_sort_2.c index 787e7972..0d1be59d 100644 --- a/sorting/bubble_sort_2.c +++ b/sorting/bubble_sort_2.c @@ -3,6 +3,7 @@ #define MAX 20 #include +#include /** * Bubble sort implementation @@ -38,6 +39,31 @@ void bubblesort(int* array_sort) } } +/** + * @brief Test function + * @returns void + */ +static void test() { + /* simple int array for testing */ + int array_sort[MAX] = {0}; + + /* populate our test array with + * random integer numbers */ + for (int i = 0; i < MAX; i++) + { + array_sort[i] = rand() % 101; + } + + /* sort array */ + bubblesort(array_sort); + + /* check if array ir correctly ordered */ + for (int i = 0; i < MAX - 1; i++) + { + assert(array_sort[i] <= array_sort[i+1]); + } +} + int main() { int i, array_sort[MAX] = {0}, is_sorted = false, change_place; From 366724b58525e1219f2abbe05a2300e305e3daec Mon Sep 17 00:00:00 2001 From: northernSage Date: Wed, 17 Feb 2021 07:15:47 -0300 Subject: [PATCH 05/10] update old main function --- sorting/bubble_sort_2.c | 41 +++++------------------------------------ 1 file changed, 5 insertions(+), 36 deletions(-) diff --git a/sorting/bubble_sort_2.c b/sorting/bubble_sort_2.c index 0d1be59d..a14b7870 100644 --- a/sorting/bubble_sort_2.c +++ b/sorting/bubble_sort_2.c @@ -64,43 +64,12 @@ static void test() { } } +/** + * @brief Main function + * @returns 0 on exit + */ int main() { - int i, array_sort[MAX] = {0}, is_sorted = false, change_place; - - /* For example - Insertion random values in array to test - */ - - for (i = 0; i < MAX; i++) - { - array_sort[i] = rand() % 101; - } - - /* Algorithm of bubble methods */ - - while (is_sorted) - { - is_sorted = false; - - for (i = 0; i < MAX - 1; i++) - { - if (array_sort[i] > array_sort[i + 1]) - { - change_place = array_sort[i]; - array_sort[i] = array_sort[i + 1]; - array_sort[i + 1] = change_place; - is_sorted = true; - } - } - } - - /* See if it works */ - - for (i = 0; i < MAX; i++) - { - printf("%d\n", array_sort[i]); - } - + test(); // call test routine return EXIT_SUCCESS; } From de09c966b33f27aceef153ea4830860065743c4e Mon Sep 17 00:00:00 2001 From: northernSage Date: Wed, 17 Feb 2021 07:17:31 -0300 Subject: [PATCH 06/10] add header comment --- sorting/bubble_sort_2.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/sorting/bubble_sort_2.c b/sorting/bubble_sort_2.c index a14b7870..72c6fca6 100644 --- a/sorting/bubble_sort_2.c +++ b/sorting/bubble_sort_2.c @@ -1,9 +1,18 @@ +/** + * @file + * @brief implementation of [Bubble sort](https://en.wikipedia.org/wiki/Bubble_sort) algorithm + * @details + * worst-case: O(n^2) + * best-case: O(n) + * average-complexity: O(n^2) + */ + #include #include -#define MAX 20 - -#include #include +#include + +#define MAX 20 /** * Bubble sort implementation From cf95c1e27a8e362d1facfec08dc1558016831707 Mon Sep 17 00:00:00 2001 From: Gabriel Fioravante Date: Wed, 17 Feb 2021 22:29:37 -0500 Subject: [PATCH 07/10] Apply suggestions from code review Co-authored-by: David Leal --- sorting/bubble_sort_2.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/sorting/bubble_sort_2.c b/sorting/bubble_sort_2.c index 72c6fca6..2813a554 100644 --- a/sorting/bubble_sort_2.c +++ b/sorting/bubble_sort_2.c @@ -5,6 +5,9 @@ * worst-case: O(n^2) * best-case: O(n) * average-complexity: O(n^2) + + * @author Unknown author + * @author [Gabriel Fioravante](https://github.com/northernSage) */ #include @@ -15,10 +18,11 @@ #define MAX 20 /** - * Bubble sort implementation + * @brief Bubble sort implementation * @param array_sort the array to be sorted + * @returns void */ -void bubblesort(int* array_sort) +void bubble_sort(int* array_sort) { bool is_sorted = false; @@ -49,7 +53,7 @@ void bubblesort(int* array_sort) } /** - * @brief Test function + * @brief Test implementations * @returns void */ static void test() { @@ -64,7 +68,7 @@ static void test() { } /* sort array */ - bubblesort(array_sort); + bubble_sort(array_sort); /* check if array ir correctly ordered */ for (int i = 0; i < MAX - 1; i++) @@ -79,6 +83,6 @@ static void test() { */ int main() { - test(); // call test routine + test(); // run self-test implementations return EXIT_SUCCESS; } From 679b30b7254d558a1baab0cc07387455d609ef8e Mon Sep 17 00:00:00 2001 From: Gabriel Fioravante Date: Wed, 17 Feb 2021 22:39:20 -0500 Subject: [PATCH 08/10] use 0 instead of EXIT_SUCCESS Co-authored-by: David Leal --- sorting/bubble_sort_2.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sorting/bubble_sort_2.c b/sorting/bubble_sort_2.c index 2813a554..ef54d3d9 100644 --- a/sorting/bubble_sort_2.c +++ b/sorting/bubble_sort_2.c @@ -84,5 +84,5 @@ static void test() { int main() { test(); // run self-test implementations - return EXIT_SUCCESS; + return 0; } From 6c6879fe52623997ce80872fd9c55b3174e4b414 Mon Sep 17 00:00:00 2001 From: northernSage Date: Thu, 18 Feb 2021 00:44:48 -0300 Subject: [PATCH 09/10] remove unused headers and add comments --- sorting/bubble_sort_2.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/sorting/bubble_sort_2.c b/sorting/bubble_sort_2.c index ef54d3d9..03dc36b4 100644 --- a/sorting/bubble_sort_2.c +++ b/sorting/bubble_sort_2.c @@ -10,10 +10,9 @@ * @author [Gabriel Fioravante](https://github.com/northernSage) */ -#include -#include -#include -#include +#include // for rand() calls +#include // for testing assert() +#include // for boolean values: true, false #define MAX 20 From 97dfc068757750b8311c5347c63c53b4f798ec8e Mon Sep 17 00:00:00 2001 From: Gabriel Fioravante Date: Wed, 17 Feb 2021 22:46:57 -0500 Subject: [PATCH 10/10] Update sorting/bubble_sort_2.c Co-authored-by: David Leal --- sorting/bubble_sort_2.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sorting/bubble_sort_2.c b/sorting/bubble_sort_2.c index 03dc36b4..26bbb526 100644 --- a/sorting/bubble_sort_2.c +++ b/sorting/bubble_sort_2.c @@ -10,9 +10,9 @@ * @author [Gabriel Fioravante](https://github.com/northernSage) */ -#include // for rand() calls -#include // for testing assert() -#include // for boolean values: true, false +#include /// for rand() calls +#include /// for assert() +#include /// for boolean values: true, false #define MAX 20