|
2 | 2 | * @file
|
3 | 3 | * @brief Bubble sort algorithm
|
4 | 4 | *
|
5 |
| - * The working principle of the Bubble sort algorithm: |
| 5 | + * @details |
| 6 | + * Bubble sort algorithm is the bubble sorting algorithm. The most important reason |
| 7 | + * for calling the bubble is that the largest number is thrown at the end of this |
| 8 | + * algorithm. This is all about the logic. In each iteration, the largest number is |
| 9 | + * expired and when iterations are completed, the sorting takes place. |
| 10 | + * |
| 11 | + * What is Swap? |
| 12 | + * |
| 13 | + * Swap in the software means that two variables are displaced. |
| 14 | + * An additional variable is required for this operation. x = 5, y = 10. |
| 15 | + * We want x = 10, y = 5. Here we create the most variable to do it. |
| 16 | + * |
| 17 | + * ```cpp |
| 18 | + * int z; |
| 19 | + * z = x; |
| 20 | + * x = y; |
| 21 | + * y = z; |
| 22 | + * ``` |
| 23 | + * |
| 24 | + * The above process is a typical displacement process. |
| 25 | + * When x assigns the value to x, the old value of x is lost. |
| 26 | + * That's why we created a variable z to create the first value of the value of x, |
| 27 | + * and finally, we have assigned to y. |
| 28 | + * |
| 29 | + * ## Bubble Sort Algorithm Analysis (Best Case - Worst Case - Average Case) |
| 30 | + * |
| 31 | + * ### Best Case |
| 32 | + * Bubble Sort Best Case Performance. \f$O(n)\f$. However, you |
| 33 | + * can't get the best status in the code we shared above. This happens on the |
| 34 | + * optimized bubble sort algorithm. It's right down there. |
| 35 | + * |
| 36 | + * ### Worst Case |
| 37 | + * Bubble Sort Worst Case Performance is \f$O(n^{2})\f$. Why is that? Because if you |
| 38 | + * remember Big O Notation, we were calculating the complexity of the algorithms in |
| 39 | + * the nested loops. The \f$n * (n - 1)\f$ product gives us \f$O(n^{2})\f$ performance. In the |
| 40 | + * worst case all the steps of the cycle will occur. |
| 41 | + * |
| 42 | + * ### Average Case |
| 43 | + * Bubble Sort is not an optimal algorithm. In average, \f$O(n^{2})\f$ performance is taken. |
| 44 | + * |
| 45 | + * @author [Deepak](https://github.com/Deepak-j-p) |
| 46 | + * @author [Nguyen Phuc Chuong](https://github.com/hollowcrust) |
| 47 | + */ |
6 | 48 |
|
7 |
| -Bubble sort algorithm is the bubble sorting algorithm. The most important reason |
8 |
| -for calling the bubble is that the largest number is thrown at the end of this |
9 |
| -algorithm. This is all about the logic. In each iteration, the largest number is |
10 |
| -expired and when iterations are completed, the sorting takes place. |
| 49 | +#include <algorithm> /// for std::is_sorted |
| 50 | +#include <cassert> /// for assert |
| 51 | +#include <iostream> /// for IO implementations |
| 52 | +#include <string> /// for std::string |
| 53 | +#include <utility> /// for std::pair, std::swap |
| 54 | +#include <vector> /// for std::vector, std::vector::push_back, std::vector::size |
11 | 55 |
|
12 |
| -What is Swap? |
| 56 | +/** |
| 57 | + * @namespace sorting |
| 58 | + * @brief Sorting algorithms |
| 59 | + */ |
| 60 | +namespace sorting { |
| 61 | +/** |
| 62 | + * @namespace bubble_sort |
| 63 | + * @brief Bubble sort algorithm |
| 64 | + */ |
| 65 | +namespace bubble_sort { |
| 66 | +/** |
| 67 | + * @brief Bubble sort algorithm |
| 68 | + * @param array An array to be sorted |
| 69 | + * @return The array sorted in ascending order |
| 70 | + */ |
| 71 | +template <typename T> |
| 72 | +std::vector<T> bubble_sort(std::vector<T>& array) { |
| 73 | + // swap_check flag to terminate the function early |
| 74 | + // if there is no swap occurs in one iteration. |
| 75 | + bool swap_check = true; |
| 76 | + int size = array.size(); |
| 77 | + for (int i = 0; (i < size) && (swap_check); i++) { |
| 78 | + swap_check = false; |
| 79 | + for (int j = 0; j < size - 1 - i; j++) { |
| 80 | + if (array[j] > array[j + 1]) { |
| 81 | + swap_check = true; |
| 82 | + std::swap(array[j], array[j + 1]); |
| 83 | + } |
| 84 | + } |
| 85 | + } |
13 | 86 |
|
14 |
| -Swap in the software means that two variables are displaced. |
15 |
| -An additional variable is required for this operation. x = 5, y = 10. |
16 |
| -We want x = 10, y = 5. Here we create the most variable to do it. |
| 87 | + return array; |
| 88 | +} |
| 89 | +} // namespace bubble_sort |
| 90 | +} // namespace sorting |
17 | 91 |
|
18 |
| -int z; |
19 |
| -z = x; |
20 |
| -x = y; |
21 |
| -y = z; |
| 92 | +/** |
| 93 | + * @brief Self-test implementation |
| 94 | + * @return void |
| 95 | + */ |
| 96 | +static void test() { |
| 97 | + std::vector<int> vec_1 = {3, 1, -9, 0}; |
| 98 | + std::vector<int> sorted_1 = sorting::bubble_sort::bubble_sort(vec_1); |
22 | 99 |
|
23 |
| -The above process is a typical displacement process. |
24 |
| -When x assigns the value to x, the old value of x is lost. |
25 |
| -That's why we created a variable z to create the first value of the value of x, |
26 |
| -and finally, we have assigned to y. |
| 100 | + std::vector<int> vec_2 = {3}; |
| 101 | + std::vector<int> sorted_2 = sorting::bubble_sort::bubble_sort(vec_2); |
27 | 102 |
|
28 |
| -Bubble Sort Algorithm Analysis (Best Case - Worst Case - Average Case) |
| 103 | + std::vector<int> vec_3 = {10, 10, 10, 10, 10}; |
| 104 | + std::vector<int> sorted_3 = sorting::bubble_sort::bubble_sort(vec_3); |
29 | 105 |
|
30 |
| -Bubble Sort Worst Case Performance is O (n²). Why is that? Because if you |
31 |
| -remember Big O Notation, we were calculating the complexity of the algorithms in |
32 |
| -the nested loops. The n * (n - 1) product gives us O (n²) performance. In the |
33 |
| -worst case all the steps of the cycle will occur. Bubble Sort (Avarage Case) |
34 |
| -Performance. Bubble Sort is not an optimal algorithm. in average, O (n²) |
35 |
| -performance is taken. Bubble Sort Best Case Performance. O (n). However, you |
36 |
| -can't get the best status in the code we shared above. This happens on the |
37 |
| -optimized bubble sort algorithm. It's right down there. |
38 |
| -*/ |
| 106 | + std::vector<float> vec_4 = {1234, -273.1, 23, 150, 1234, 1555.55, -2000}; |
| 107 | + std::vector<float> sorted_4 = sorting::bubble_sort::bubble_sort(vec_4); |
39 | 108 |
|
40 |
| -#include <iostream> |
41 |
| -#include <vector> |
| 109 | + std::vector<char> vec_5 = {'z', 'Z', 'a', 'B', ' ', 'c', 'a'}; |
| 110 | + std::vector<char> sorted_5 = sorting::bubble_sort::bubble_sort(vec_5); |
42 | 111 |
|
43 |
| -int main() { |
44 |
| - int n; |
45 |
| - bool swap_check = true; |
46 |
| - std::cout << "Enter the amount of numbers to sort: "; |
47 |
| - std::cin >> n; |
48 |
| - std::vector<int> numbers; |
49 |
| - std::cout << "Enter " << n << " numbers: "; |
50 |
| - int num; |
| 112 | + std::vector<std::string> vec_6 = {"Hello", "hello", "Helo", "Hi", "hehe"}; |
| 113 | + std::vector<std::string> sorted_6 = sorting::bubble_sort::bubble_sort(vec_6); |
51 | 114 |
|
52 |
| - // Input |
53 |
| - for (int i = 0; i < n; i++) { |
54 |
| - std::cin >> num; |
55 |
| - numbers.push_back(num); |
56 |
| - } |
| 115 | + std::vector<std::pair<int, char>> vec_7 = {{10, 'c'}, {2, 'z'}, {10, 'a'}, {0, 'b'}, {-1, 'z'}}; |
| 116 | + std::vector<std::pair<int, char>> sorted_7 = sorting::bubble_sort::bubble_sort(vec_7); |
57 | 117 |
|
58 |
| - // Bubble Sorting |
59 |
| - for (int i = 0; (i < n) && (swap_check); i++) { |
60 |
| - swap_check = false; |
61 |
| - for (int j = 0; j < n - 1 - i; j++) { |
62 |
| - if (numbers[j] > numbers[j + 1]) { |
63 |
| - swap_check = true; |
64 |
| - std::swap(numbers[j], |
65 |
| - numbers[j + 1]); // by changing swap location. |
66 |
| - // I mean, j. If the number is |
67 |
| - // greater than j + 1, then it |
68 |
| - // means the location. |
69 |
| - } |
70 |
| - } |
71 |
| - } |
| 118 | + assert(std::is_sorted(sorted_1.begin(), sorted_1.end())); |
| 119 | + assert(std::is_sorted(sorted_2.begin(), sorted_2.end())); |
| 120 | + assert(std::is_sorted(sorted_3.begin(), sorted_3.end())); |
| 121 | + assert(std::is_sorted(sorted_4.begin(), sorted_4.end())); |
| 122 | + assert(std::is_sorted(sorted_5.begin(), sorted_5.end())); |
| 123 | + assert(std::is_sorted(sorted_6.begin(), sorted_6.end())); |
| 124 | + assert(std::is_sorted(sorted_7.begin(), sorted_7.end())); |
| 125 | +} |
72 | 126 |
|
73 |
| - // Output |
74 |
| - std::cout << "\nSorted Array : "; |
75 |
| - for (int i = 0; i < numbers.size(); i++) { |
76 |
| - if (i != numbers.size() - 1) { |
77 |
| - std::cout << numbers[i] << ", "; |
78 |
| - } else { |
79 |
| - std::cout << numbers[i] << std::endl; |
80 |
| - } |
81 |
| - } |
82 |
| - return 0; |
| 127 | +/** |
| 128 | + * @brief Main function |
| 129 | + * @return 0 on exit |
| 130 | + */ |
| 131 | +int main() { |
| 132 | + test(); |
| 133 | + return 0; |
83 | 134 | }
|
0 commit comments