|
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 | + * int z; |
| 18 | + * z = x; |
| 19 | + * x = y; |
| 20 | + * y = z; |
| 21 | + * |
| 22 | + * The above process is a typical displacement process. |
| 23 | + * When x assigns the value to x, the old value of x is lost. |
| 24 | + * That's why we created a variable z to create the first value of the value of x, |
| 25 | + * and finally, we have assigned to y. |
| 26 | + * |
| 27 | + * Bubble Sort Algorithm Analysis (Best Case - Worst Case - Average Case) |
| 28 | + * |
| 29 | + * Bubble Sort Worst Case Performance is O (n²). Why is that? Because if you |
| 30 | + * remember Big O Notation, we were calculating the complexity of the algorithms in |
| 31 | + * the nested loops. The n * (n - 1) product gives us O (n²) performance. In the |
| 32 | + * worst case all the steps of the cycle will occur. Bubble Sort (Avarage Case) |
| 33 | + * Performance. Bubble Sort is not an optimal algorithm. in average, O (n²) |
| 34 | + * performance is taken. Bubble Sort Best Case Performance. O (n). However, you |
| 35 | + * can't get the best status in the code we shared above. This happens on the |
| 36 | + * optimized bubble sort algorithm. It's right down there. |
| 37 | + */ |
6 | 38 |
|
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. |
| 39 | +#include <cassert> /// for assert |
| 40 | +#include <iostream> /// for IO implementations |
| 41 | +#include <string> /// for std::string |
| 42 | +#include <utility> /// for std::pair, std::swap |
| 43 | +#include <vector> /// for std::vector, std::vector::push_back, std::vector::size |
11 | 44 |
|
12 |
| -What is Swap? |
| 45 | +/** |
| 46 | + * @namespace sorting |
| 47 | + * @brief Sorting algorithms |
| 48 | + */ |
| 49 | +namespace sorting { |
| 50 | +/** |
| 51 | + * @namespace bubble_sort |
| 52 | + * @brief Bubble sort algorithm |
| 53 | + */ |
| 54 | +namespace bubble_sort { |
| 55 | +/** |
| 56 | + * @brief Bubble sort algorithm |
| 57 | + * @param array An array to be sorted |
| 58 | + * @return The array sorted in ascending order |
| 59 | + */ |
| 60 | +template <typename T> |
| 61 | +std::vector<T> bubble_sort(std::vector<T>& array) { |
| 62 | + // swap_check flag to terminate the function early |
| 63 | + // if there is no swap occurs in one iteration. |
| 64 | + bool swap_check = true; |
| 65 | + int size = array.size(); |
| 66 | + for (int i = 0; (i < size) && (swap_check); i++) { |
| 67 | + swap_check = false; |
| 68 | + for (int j = 0; j < size - 1 - i; j++) { |
| 69 | + if (array[j] > array[j + 1]) { |
| 70 | + swap_check = true; |
| 71 | + std::swap(array[j], array[j + 1]); |
| 72 | + } |
| 73 | + } |
| 74 | + } |
13 | 75 |
|
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. |
| 76 | + return array; |
| 77 | +} |
| 78 | +} // namespace bubble_sort |
| 79 | +} // namespace sorting |
17 | 80 |
|
18 |
| -int z; |
19 |
| -z = x; |
20 |
| -x = y; |
21 |
| -y = z; |
| 81 | +/** |
| 82 | + * @brief Self-test implementation |
| 83 | + * @return void |
| 84 | + */ |
| 85 | +static void test() { |
| 86 | + std::vector<int> vec_1 = {3, 1, -9, 0}; |
| 87 | + std::vector<int> sorted_1 = {-9, 0, 1, 3}; |
22 | 88 |
|
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. |
| 89 | + std::vector<int> vec_2 = {3}; |
| 90 | + std::vector<int> sorted_2 = {3}; |
27 | 91 |
|
28 |
| -Bubble Sort Algorithm Analysis (Best Case - Worst Case - Average Case) |
| 92 | + std::vector<int> vec_3 = {10, 10, 10, 10, 10}; |
| 93 | + std::vector<int> sorted_3 = {10, 10, 10, 10, 10}; |
29 | 94 |
|
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 |
| -*/ |
| 95 | + std::vector<float> vec_4 = {1234, -273.1, 23, 150, 1234, 1555.55, -2000}; |
| 96 | + std::vector<float> sorted_4 = {-2000, -273.1, 23, 150, 1234, 1234, 1555.55}; |
39 | 97 |
|
40 |
| -#include <iostream> |
41 |
| -#include <vector> |
| 98 | + std::vector<char> vec_5 = {'z', 'Z', 'a', 'B', ' ', 'c', 'a'}; |
| 99 | + std::vector<char> sorted_5 = {' ', 'B', 'Z', 'a', 'a', 'c', 'z'}; |
42 | 100 |
|
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; |
| 101 | + std::vector<std::string> vec_6 = {"Hello", "hello", "Helo", "Hi", "hehe"}; |
| 102 | + std::vector<std::string> sorted_6 = {"Hello", "Helo", "Hi", "hehe", "hello"}; |
51 | 103 |
|
52 |
| - // Input |
53 |
| - for (int i = 0; i < n; i++) { |
54 |
| - std::cin >> num; |
55 |
| - numbers.push_back(num); |
56 |
| - } |
| 104 | + std::vector<std::pair<int, char>> vec_7 = {{10, 'c'}, {2, 'z'}, {10, 'a'}, {0, 'b'}, {-1, 'z'}}; |
| 105 | + std::vector<std::pair<int, char>> sorted_7 = {{-1, 'z'}, {0, 'b'}, {2, 'z'}, {10, 'a'}, {10, 'c'}}; |
57 | 106 |
|
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 |
| - } |
| 107 | + assert((sorting::bubble_sort::bubble_sort(vec_1) == sorted_1)); |
| 108 | + assert((sorting::bubble_sort::bubble_sort(vec_2) == sorted_2)); |
| 109 | + assert((sorting::bubble_sort::bubble_sort(vec_3) == sorted_3)); |
| 110 | + assert((sorting::bubble_sort::bubble_sort(vec_4) == sorted_4)); |
| 111 | + assert((sorting::bubble_sort::bubble_sort(vec_5) == sorted_5)); |
| 112 | + assert((sorting::bubble_sort::bubble_sort(vec_6) == sorted_6)); |
| 113 | + assert((sorting::bubble_sort::bubble_sort(vec_7) == sorted_7)); |
| 114 | +} |
72 | 115 |
|
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; |
| 116 | +/** |
| 117 | + * @brief Main program |
| 118 | + * @return 0 on exit |
| 119 | + */ |
| 120 | +int main() { |
| 121 | + test(); |
| 122 | + return 0; |
83 | 123 | }
|
0 commit comments