From 6777475787995943ad3c9edda0a70772dc0f532a Mon Sep 17 00:00:00 2001 From: Nguyen Phuc Chuong <72879387+hollowcrust@users.noreply.github.com> Date: Wed, 9 Oct 2024 19:48:01 +0800 Subject: [PATCH 1/6] Update bubble_sort.cpp --- sorting/bubble_sort.cpp | 172 +++++++++++++++++++++++++--------------- 1 file changed, 106 insertions(+), 66 deletions(-) diff --git a/sorting/bubble_sort.cpp b/sorting/bubble_sort.cpp index c43e425fcd9..a3c401649c9 100644 --- a/sorting/bubble_sort.cpp +++ b/sorting/bubble_sort.cpp @@ -2,82 +2,122 @@ * @file * @brief Bubble sort algorithm * - * The working principle of the Bubble sort algorithm: + * @details + * Bubble sort algorithm is the bubble sorting algorithm. The most important reason + * for calling the bubble is that the largest number is thrown at the end of this + * algorithm. This is all about the logic. In each iteration, the largest number is + * expired and when iterations are completed, the sorting takes place. + * + * What is Swap? + * + * Swap in the software means that two variables are displaced. + * An additional variable is required for this operation. x = 5, y = 10. + * We want x = 10, y = 5. Here we create the most variable to do it. + * + * int z; + * z = x; + * x = y; + * y = z; + * + * The above process is a typical displacement process. + * When x assigns the value to x, the old value of x is lost. + * That's why we created a variable z to create the first value of the value of x, + * and finally, we have assigned to y. + * + * Bubble Sort Algorithm Analysis (Best Case - Worst Case - Average Case) + * + * Bubble Sort Worst Case Performance is O (n²). Why is that? Because if you + * remember Big O Notation, we were calculating the complexity of the algorithms in + * the nested loops. The n * (n - 1) product gives us O (n²) performance. In the + * worst case all the steps of the cycle will occur. Bubble Sort (Avarage Case) + * Performance. Bubble Sort is not an optimal algorithm. in average, O (n²) + * performance is taken. Bubble Sort Best Case Performance. O (n). However, you + * can't get the best status in the code we shared above. This happens on the + * optimized bubble sort algorithm. It's right down there. + */ -Bubble sort algorithm is the bubble sorting algorithm. The most important reason -for calling the bubble is that the largest number is thrown at the end of this -algorithm. This is all about the logic. In each iteration, the largest number is -expired and when iterations are completed, the sorting takes place. +#include /// for assert +#include /// for IO implementations +#include /// for std::string +#include /// for std::pair, std::swap +#include /// for std::vector, std::vector::push_back, std::vector::size -What is Swap? +/** + * @namespace sorting + * @brief Sorting algorithms + */ +namespace sorting { +/** + * @namespace bubble_sort + * @brief Bubble sort algorithm + */ +namespace bubble_sort { +/** + * @brief Bubble sort algorithm + * @param array An array to be sorted + * @return The array sorted in ascending order + */ +template +std::vector bubble_sort(std::vector& array) { + // swap_check flag to terminate the function early + // if there is no swap occurs in one iteration. + bool swap_check = true; + int size = array.size(); + for (int i = 0; (i < size) && (swap_check); i++) { + swap_check = false; + for (int j = 0; j < size - 1 - i; j++) { + if (array[j] > array[j + 1]) { + swap_check = true; + std::swap(array[j], array[j + 1]); + } + } + } -Swap in the software means that two variables are displaced. -An additional variable is required for this operation. x = 5, y = 10. -We want x = 10, y = 5. Here we create the most variable to do it. + return array; +} +} // namespace bubble_sort +} // namespace sorting -int z; -z = x; -x = y; -y = z; +/** + * @brief Self-test implementation + * @return void + */ +static void test() { + std::vector vec_1 = {3, 1, -9, 0}; + std::vector sorted_1 = {-9, 0, 1, 3}; -The above process is a typical displacement process. -When x assigns the value to x, the old value of x is lost. -That's why we created a variable z to create the first value of the value of x, -and finally, we have assigned to y. + std::vector vec_2 = {3}; + std::vector sorted_2 = {3}; -Bubble Sort Algorithm Analysis (Best Case - Worst Case - Average Case) + std::vector vec_3 = {10, 10, 10, 10, 10}; + std::vector sorted_3 = {10, 10, 10, 10, 10}; -Bubble Sort Worst Case Performance is O (n²). Why is that? Because if you -remember Big O Notation, we were calculating the complexity of the algorithms in -the nested loops. The n * (n - 1) product gives us O (n²) performance. In the -worst case all the steps of the cycle will occur. Bubble Sort (Avarage Case) -Performance. Bubble Sort is not an optimal algorithm. in average, O (n²) -performance is taken. Bubble Sort Best Case Performance. O (n). However, you -can't get the best status in the code we shared above. This happens on the -optimized bubble sort algorithm. It's right down there. -*/ + std::vector vec_4 = {1234, -273.1, 23, 150, 1234, 1555.55, -2000}; + std::vector sorted_4 = {-2000, -273.1, 23, 150, 1234, 1234, 1555.55}; -#include -#include + std::vector vec_5 = {'z', 'Z', 'a', 'B', ' ', 'c', 'a'}; + std::vector sorted_5 = {' ', 'B', 'Z', 'a', 'a', 'c', 'z'}; -int main() { - int n; - bool swap_check = true; - std::cout << "Enter the amount of numbers to sort: "; - std::cin >> n; - std::vector numbers; - std::cout << "Enter " << n << " numbers: "; - int num; + std::vector vec_6 = {"Hello", "hello", "Helo", "Hi", "hehe"}; + std::vector sorted_6 = {"Hello", "Helo", "Hi", "hehe", "hello"}; - // Input - for (int i = 0; i < n; i++) { - std::cin >> num; - numbers.push_back(num); - } + std::vector> vec_7 = {{10, 'c'}, {2, 'z'}, {10, 'a'}, {0, 'b'}, {-1, 'z'}}; + std::vector> sorted_7 = {{-1, 'z'}, {0, 'b'}, {2, 'z'}, {10, 'a'}, {10, 'c'}}; - // Bubble Sorting - for (int i = 0; (i < n) && (swap_check); i++) { - swap_check = false; - for (int j = 0; j < n - 1 - i; j++) { - if (numbers[j] > numbers[j + 1]) { - swap_check = true; - std::swap(numbers[j], - numbers[j + 1]); // by changing swap location. - // I mean, j. If the number is - // greater than j + 1, then it - // means the location. - } - } - } + assert((sorting::bubble_sort::bubble_sort(vec_1) == sorted_1)); + assert((sorting::bubble_sort::bubble_sort(vec_2) == sorted_2)); + assert((sorting::bubble_sort::bubble_sort(vec_3) == sorted_3)); + assert((sorting::bubble_sort::bubble_sort(vec_4) == sorted_4)); + assert((sorting::bubble_sort::bubble_sort(vec_5) == sorted_5)); + assert((sorting::bubble_sort::bubble_sort(vec_6) == sorted_6)); + assert((sorting::bubble_sort::bubble_sort(vec_7) == sorted_7)); +} - // Output - std::cout << "\nSorted Array : "; - for (int i = 0; i < numbers.size(); i++) { - if (i != numbers.size() - 1) { - std::cout << numbers[i] << ", "; - } else { - std::cout << numbers[i] << std::endl; - } - } - return 0; +/** + * @brief Main program + * @return 0 on exit + */ +int main() { + test(); + return 0; } From 49f568f9960e10d3d24e97732a74630a6075b89d Mon Sep 17 00:00:00 2001 From: Nguyen Phuc Chuong <72879387+hollowcrust@users.noreply.github.com> Date: Wed, 9 Oct 2024 19:56:05 +0800 Subject: [PATCH 2/6] Update bubble_sort.cpp --- sorting/bubble_sort.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sorting/bubble_sort.cpp b/sorting/bubble_sort.cpp index a3c401649c9..1323ced5d30 100644 --- a/sorting/bubble_sort.cpp +++ b/sorting/bubble_sort.cpp @@ -114,7 +114,7 @@ static void test() { } /** - * @brief Main program + * @brief Main function * @return 0 on exit */ int main() { From 4f12b9c39cd181bd25c25f3e28e00b7a69fda582 Mon Sep 17 00:00:00 2001 From: Nguyen Phuc Chuong <72879387+hollowcrust@users.noreply.github.com> Date: Wed, 9 Oct 2024 20:28:46 +0800 Subject: [PATCH 3/6] Update bubble_sort.cpp Add latex notation --- sorting/bubble_sort.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/sorting/bubble_sort.cpp b/sorting/bubble_sort.cpp index 1323ced5d30..800a026e295 100644 --- a/sorting/bubble_sort.cpp +++ b/sorting/bubble_sort.cpp @@ -26,12 +26,12 @@ * * Bubble Sort Algorithm Analysis (Best Case - Worst Case - Average Case) * - * Bubble Sort Worst Case Performance is O (n²). Why is that? Because if you + * Bubble Sort Worst Case Performance is \f$O(n^{2})\f$. Why is that? Because if you * remember Big O Notation, we were calculating the complexity of the algorithms in - * the nested loops. The n * (n - 1) product gives us O (n²) performance. In the + * the nested loops. The \f$n * (n - 1)\f$ product gives us \f$O(n^{2})\f$ performance. In the * worst case all the steps of the cycle will occur. Bubble Sort (Avarage Case) - * Performance. Bubble Sort is not an optimal algorithm. in average, O (n²) - * performance is taken. Bubble Sort Best Case Performance. O (n). However, you + * Performance. Bubble Sort is not an optimal algorithm. in average, \f$O(n^{2})\f$ + * performance is taken. Bubble Sort Best Case Performance. \f$O(n)\f$. However, you * can't get the best status in the code we shared above. This happens on the * optimized bubble sort algorithm. It's right down there. */ From 6f74a5aa872a1b34e8cbbcfafb3a12b48d0d4938 Mon Sep 17 00:00:00 2001 From: Nguyen Phuc Chuong <72879387+hollowcrust@users.noreply.github.com> Date: Wed, 9 Oct 2024 22:30:39 +0800 Subject: [PATCH 4/6] Update sorting/bubble_sort.cpp Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> --- sorting/bubble_sort.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sorting/bubble_sort.cpp b/sorting/bubble_sort.cpp index 800a026e295..5e4ab2735b9 100644 --- a/sorting/bubble_sort.cpp +++ b/sorting/bubble_sort.cpp @@ -14,10 +14,12 @@ * An additional variable is required for this operation. x = 5, y = 10. * We want x = 10, y = 5. Here we create the most variable to do it. * + * ```cpp * int z; * z = x; * x = y; * y = z; + * ``` * * The above process is a typical displacement process. * When x assigns the value to x, the old value of x is lost. From 010ff9391e28eec409aa640343b9983783877e0f Mon Sep 17 00:00:00 2001 From: Nguyen Phuc Chuong <72879387+hollowcrust@users.noreply.github.com> Date: Wed, 9 Oct 2024 22:31:12 +0800 Subject: [PATCH 5/6] Update sorting/bubble_sort.cpp Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> --- sorting/bubble_sort.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sorting/bubble_sort.cpp b/sorting/bubble_sort.cpp index 5e4ab2735b9..34f44b49343 100644 --- a/sorting/bubble_sort.cpp +++ b/sorting/bubble_sort.cpp @@ -26,7 +26,7 @@ * That's why we created a variable z to create the first value of the value of x, * and finally, we have assigned to y. * - * Bubble Sort Algorithm Analysis (Best Case - Worst Case - Average Case) + * ## Bubble Sort Algorithm Analysis (Best Case - Worst Case - Average Case) * * Bubble Sort Worst Case Performance is \f$O(n^{2})\f$. Why is that? Because if you * remember Big O Notation, we were calculating the complexity of the algorithms in From 098cdda43c3240a788d8bef2e77fe4941e75d257 Mon Sep 17 00:00:00 2001 From: Nguyen Phuc Chuong <72879387+hollowcrust@users.noreply.github.com> Date: Wed, 9 Oct 2024 23:58:12 +0800 Subject: [PATCH 6/6] Update bubble_sort.cpp --- sorting/bubble_sort.cpp | 57 ++++++++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 24 deletions(-) diff --git a/sorting/bubble_sort.cpp b/sorting/bubble_sort.cpp index 34f44b49343..9209f8664e7 100644 --- a/sorting/bubble_sort.cpp +++ b/sorting/bubble_sort.cpp @@ -28,21 +28,30 @@ * * ## Bubble Sort Algorithm Analysis (Best Case - Worst Case - Average Case) * + * ### Best Case + * Bubble Sort Best Case Performance. \f$O(n)\f$. However, you + * can't get the best status in the code we shared above. This happens on the + * optimized bubble sort algorithm. It's right down there. + * + * ### Worst Case * Bubble Sort Worst Case Performance is \f$O(n^{2})\f$. Why is that? Because if you * remember Big O Notation, we were calculating the complexity of the algorithms in * the nested loops. The \f$n * (n - 1)\f$ product gives us \f$O(n^{2})\f$ performance. In the - * worst case all the steps of the cycle will occur. Bubble Sort (Avarage Case) - * Performance. Bubble Sort is not an optimal algorithm. in average, \f$O(n^{2})\f$ - * performance is taken. Bubble Sort Best Case Performance. \f$O(n)\f$. However, you - * can't get the best status in the code we shared above. This happens on the - * optimized bubble sort algorithm. It's right down there. + * worst case all the steps of the cycle will occur. + * + * ### Average Case + * Bubble Sort is not an optimal algorithm. In average, \f$O(n^{2})\f$ performance is taken. + * + * @author [Deepak](https://github.com/Deepak-j-p) + * @author [Nguyen Phuc Chuong](https://github.com/hollowcrust) */ -#include /// for assert -#include /// for IO implementations -#include /// for std::string -#include /// for std::pair, std::swap -#include /// for std::vector, std::vector::push_back, std::vector::size +#include /// for std::is_sorted +#include /// for assert +#include /// for IO implementations +#include /// for std::string +#include /// for std::pair, std::swap +#include /// for std::vector, std::vector::push_back, std::vector::size /** * @namespace sorting @@ -86,33 +95,33 @@ std::vector bubble_sort(std::vector& array) { */ static void test() { std::vector vec_1 = {3, 1, -9, 0}; - std::vector sorted_1 = {-9, 0, 1, 3}; + std::vector sorted_1 = sorting::bubble_sort::bubble_sort(vec_1); std::vector vec_2 = {3}; - std::vector sorted_2 = {3}; + std::vector sorted_2 = sorting::bubble_sort::bubble_sort(vec_2); std::vector vec_3 = {10, 10, 10, 10, 10}; - std::vector sorted_3 = {10, 10, 10, 10, 10}; + std::vector sorted_3 = sorting::bubble_sort::bubble_sort(vec_3); std::vector vec_4 = {1234, -273.1, 23, 150, 1234, 1555.55, -2000}; - std::vector sorted_4 = {-2000, -273.1, 23, 150, 1234, 1234, 1555.55}; + std::vector sorted_4 = sorting::bubble_sort::bubble_sort(vec_4); std::vector vec_5 = {'z', 'Z', 'a', 'B', ' ', 'c', 'a'}; - std::vector sorted_5 = {' ', 'B', 'Z', 'a', 'a', 'c', 'z'}; + std::vector sorted_5 = sorting::bubble_sort::bubble_sort(vec_5); std::vector vec_6 = {"Hello", "hello", "Helo", "Hi", "hehe"}; - std::vector sorted_6 = {"Hello", "Helo", "Hi", "hehe", "hello"}; + std::vector sorted_6 = sorting::bubble_sort::bubble_sort(vec_6); std::vector> vec_7 = {{10, 'c'}, {2, 'z'}, {10, 'a'}, {0, 'b'}, {-1, 'z'}}; - std::vector> sorted_7 = {{-1, 'z'}, {0, 'b'}, {2, 'z'}, {10, 'a'}, {10, 'c'}}; + std::vector> sorted_7 = sorting::bubble_sort::bubble_sort(vec_7); - assert((sorting::bubble_sort::bubble_sort(vec_1) == sorted_1)); - assert((sorting::bubble_sort::bubble_sort(vec_2) == sorted_2)); - assert((sorting::bubble_sort::bubble_sort(vec_3) == sorted_3)); - assert((sorting::bubble_sort::bubble_sort(vec_4) == sorted_4)); - assert((sorting::bubble_sort::bubble_sort(vec_5) == sorted_5)); - assert((sorting::bubble_sort::bubble_sort(vec_6) == sorted_6)); - assert((sorting::bubble_sort::bubble_sort(vec_7) == sorted_7)); + assert(std::is_sorted(sorted_1.begin(), sorted_1.end())); + assert(std::is_sorted(sorted_2.begin(), sorted_2.end())); + assert(std::is_sorted(sorted_3.begin(), sorted_3.end())); + assert(std::is_sorted(sorted_4.begin(), sorted_4.end())); + assert(std::is_sorted(sorted_5.begin(), sorted_5.end())); + assert(std::is_sorted(sorted_6.begin(), sorted_6.end())); + assert(std::is_sorted(sorted_7.begin(), sorted_7.end())); } /**