Skip to content

Commit 098cdda

Browse files
authored
Update bubble_sort.cpp
1 parent 8e115d7 commit 098cdda

File tree

1 file changed

+33
-24
lines changed

1 file changed

+33
-24
lines changed

sorting/bubble_sort.cpp

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,30 @@
2828
*
2929
* ## Bubble Sort Algorithm Analysis (Best Case - Worst Case - Average Case)
3030
*
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
3137
* Bubble Sort Worst Case Performance is \f$O(n^{2})\f$. Why is that? Because if you
3238
* remember Big O Notation, we were calculating the complexity of the algorithms in
3339
* 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)
3947
*/
4048

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
4655

4756
/**
4857
* @namespace sorting
@@ -86,33 +95,33 @@ std::vector<T> bubble_sort(std::vector<T>& array) {
8695
*/
8796
static void test() {
8897
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);
9099

91100
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);
93102

94103
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);
96105

97106
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);
99108

100109
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);
102111

103112
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);
105114

106115
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);
108117

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()));
116125
}
117126

118127
/**

0 commit comments

Comments
 (0)