|
28 | 28 | *
|
29 | 29 | * ## Bubble Sort Algorithm Analysis (Best Case - Worst Case - Average Case)
|
30 | 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 |
31 | 37 | * Bubble Sort Worst Case Performance is \f$O(n^{2})\f$. Why is that? Because if you
|
32 | 38 | * remember Big O Notation, we were calculating the complexity of the algorithms in
|
33 | 39 | * the nested loops. The \f$n * (n - 1)\f$ product gives us \f$O(n^{2})\f$ performance. In the
|
34 |
| - * worst case all the steps of the cycle will occur. Bubble Sort (Avarage Case) |
35 |
| - * Performance. Bubble Sort is not an optimal algorithm. in average, \f$O(n^{2})\f$ |
36 |
| - * performance is taken. Bubble Sort Best Case Performance. \f$O(n)\f$. However, you |
37 |
| - * can't get the best status in the code we shared above. This happens on the |
38 |
| - * optimized bubble sort algorithm. It's right down there. |
| 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) |
39 | 47 | */
|
40 | 48 |
|
41 |
| -#include <cassert> /// for assert |
42 |
| -#include <iostream> /// for IO implementations |
43 |
| -#include <string> /// for std::string |
44 |
| -#include <utility> /// for std::pair, std::swap |
45 |
| -#include <vector> /// for std::vector, std::vector::push_back, std::vector::size |
| 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 |
46 | 55 |
|
47 | 56 | /**
|
48 | 57 | * @namespace sorting
|
@@ -86,33 +95,33 @@ std::vector<T> bubble_sort(std::vector<T>& array) {
|
86 | 95 | */
|
87 | 96 | static void test() {
|
88 | 97 | std::vector<int> vec_1 = {3, 1, -9, 0};
|
89 |
| - std::vector<int> sorted_1 = {-9, 0, 1, 3}; |
| 98 | + std::vector<int> sorted_1 = sorting::bubble_sort::bubble_sort(vec_1); |
90 | 99 |
|
91 | 100 | std::vector<int> vec_2 = {3};
|
92 |
| - std::vector<int> sorted_2 = {3}; |
| 101 | + std::vector<int> sorted_2 = sorting::bubble_sort::bubble_sort(vec_2); |
93 | 102 |
|
94 | 103 | std::vector<int> vec_3 = {10, 10, 10, 10, 10};
|
95 |
| - std::vector<int> sorted_3 = {10, 10, 10, 10, 10}; |
| 104 | + std::vector<int> sorted_3 = sorting::bubble_sort::bubble_sort(vec_3); |
96 | 105 |
|
97 | 106 | std::vector<float> vec_4 = {1234, -273.1, 23, 150, 1234, 1555.55, -2000};
|
98 |
| - std::vector<float> sorted_4 = {-2000, -273.1, 23, 150, 1234, 1234, 1555.55}; |
| 107 | + std::vector<float> sorted_4 = sorting::bubble_sort::bubble_sort(vec_4); |
99 | 108 |
|
100 | 109 | std::vector<char> vec_5 = {'z', 'Z', 'a', 'B', ' ', 'c', 'a'};
|
101 |
| - std::vector<char> sorted_5 = {' ', 'B', 'Z', 'a', 'a', 'c', 'z'}; |
| 110 | + std::vector<char> sorted_5 = sorting::bubble_sort::bubble_sort(vec_5); |
102 | 111 |
|
103 | 112 | std::vector<std::string> vec_6 = {"Hello", "hello", "Helo", "Hi", "hehe"};
|
104 |
| - std::vector<std::string> sorted_6 = {"Hello", "Helo", "Hi", "hehe", "hello"}; |
| 113 | + std::vector<std::string> sorted_6 = sorting::bubble_sort::bubble_sort(vec_6); |
105 | 114 |
|
106 | 115 | std::vector<std::pair<int, char>> vec_7 = {{10, 'c'}, {2, 'z'}, {10, 'a'}, {0, 'b'}, {-1, 'z'}};
|
107 |
| - std::vector<std::pair<int, char>> sorted_7 = {{-1, 'z'}, {0, 'b'}, {2, 'z'}, {10, 'a'}, {10, 'c'}}; |
| 116 | + std::vector<std::pair<int, char>> sorted_7 = sorting::bubble_sort::bubble_sort(vec_7); |
108 | 117 |
|
109 |
| - assert((sorting::bubble_sort::bubble_sort(vec_1) == sorted_1)); |
110 |
| - assert((sorting::bubble_sort::bubble_sort(vec_2) == sorted_2)); |
111 |
| - assert((sorting::bubble_sort::bubble_sort(vec_3) == sorted_3)); |
112 |
| - assert((sorting::bubble_sort::bubble_sort(vec_4) == sorted_4)); |
113 |
| - assert((sorting::bubble_sort::bubble_sort(vec_5) == sorted_5)); |
114 |
| - assert((sorting::bubble_sort::bubble_sort(vec_6) == sorted_6)); |
115 |
| - assert((sorting::bubble_sort::bubble_sort(vec_7) == sorted_7)); |
| 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())); |
116 | 125 | }
|
117 | 126 |
|
118 | 127 | /**
|
|
0 commit comments