2016-07-16 18:56:48 +08:00
|
|
|
//Bubble Sort
|
|
|
|
|
2019-08-21 10:10:08 +08:00
|
|
|
#include <iostream>
|
|
|
|
#include <vector>
|
2016-07-16 18:56:48 +08:00
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
2017-05-27 20:40:04 +08:00
|
|
|
int n;
|
2019-09-18 00:19:29 +08:00
|
|
|
short swap_check = 1;
|
2019-08-21 10:10:08 +08:00
|
|
|
cout << "Enter the amount of numbers to sort: ";
|
2017-05-27 20:40:04 +08:00
|
|
|
cin >> n;
|
2018-12-22 13:01:52 +08:00
|
|
|
vector<int> numbers;
|
|
|
|
cout << "Enter " << n << " numbers: ";
|
|
|
|
int num;
|
2017-05-27 20:40:04 +08:00
|
|
|
|
2019-08-21 10:10:08 +08:00
|
|
|
//Input
|
|
|
|
for (int i = 0; i < n; i++)
|
2016-07-16 18:56:48 +08:00
|
|
|
{
|
2018-12-22 13:01:52 +08:00
|
|
|
cin >> num;
|
|
|
|
numbers.push_back(num);
|
2016-07-16 18:56:48 +08:00
|
|
|
}
|
2017-05-27 20:40:04 +08:00
|
|
|
|
2016-07-16 18:56:48 +08:00
|
|
|
//Bubble Sorting
|
2019-09-18 00:19:29 +08:00
|
|
|
for (int i = 0; (i < n) && (swap_check == 1); i++)
|
2016-07-16 18:56:48 +08:00
|
|
|
{
|
2019-08-21 10:10:08 +08:00
|
|
|
swap_check = 0;
|
|
|
|
for (int j = 0; j < n - 1 - i; j++)
|
2016-07-16 18:56:48 +08:00
|
|
|
{
|
2019-08-21 10:10:08 +08:00
|
|
|
if (numbers[j] > numbers[j + 1])
|
2016-07-16 18:56:48 +08:00
|
|
|
{
|
2019-08-21 10:10:08 +08:00
|
|
|
swap_check = 1;
|
2020-04-24 16:14:41 +08:00
|
|
|
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.
|
2016-07-16 18:56:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-05-27 20:40:04 +08:00
|
|
|
|
2016-07-16 18:56:48 +08:00
|
|
|
//Output
|
2019-08-21 10:10:08 +08:00
|
|
|
cout << "\nSorted Array : ";
|
|
|
|
for (int i = 0; i < numbers.size(); i++)
|
2016-07-16 18:56:48 +08:00
|
|
|
{
|
2019-08-21 10:10:08 +08:00
|
|
|
if (i != numbers.size() - 1)
|
2018-12-22 13:01:52 +08:00
|
|
|
{
|
|
|
|
cout << numbers[i] << ", ";
|
2019-08-21 10:10:08 +08:00
|
|
|
}
|
|
|
|
else
|
2018-12-22 13:01:52 +08:00
|
|
|
{
|
|
|
|
cout << numbers[i] << endl;
|
|
|
|
}
|
2016-07-16 18:56:48 +08:00
|
|
|
}
|
2020-04-24 16:18:19 +08:00
|
|
|
return 0;
|
2016-07-16 18:56:48 +08:00
|
|
|
}
|
2019-05-14 19:08:21 +08:00
|
|
|
|
2019-05-17 21:28:23 +08:00
|
|
|
/*The working principle of the Bubble sort algorithm:
|
2019-05-14 19:08:21 +08:00
|
|
|
|
2019-05-17 21:28:23 +08:00
|
|
|
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.
|
2019-05-14 19:08:21 +08:00
|
|
|
|
2019-05-17 21:28:23 +08:00
|
|
|
What is Swap?
|
2019-05-14 19:08:21 +08:00
|
|
|
|
2019-05-17 21:28:23 +08:00
|
|
|
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.
|
2019-05-14 19:08:21 +08:00
|
|
|
|
|
|
|
int z;
|
|
|
|
z = x;
|
|
|
|
x = y;
|
|
|
|
y = z;
|
|
|
|
|
2019-05-17 21:28:23 +08:00
|
|
|
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.
|
2019-05-14 19:08:21 +08:00
|
|
|
|
2019-05-17 21:28:23 +08:00
|
|
|
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.
|
|
|
|
* /
|