-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBubbleSort.cpp
55 lines (51 loc) · 1.26 KB
/
BubbleSort.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*Bubble sort implementation.
In each iteration element is checked with its adjacent element, if the element is greater than its adjacent then they are swapped.
This way the largest element "bubbles up" to the last position ,second largest to second last position and so on.
Complexity is O(n^2) */
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <vector>
#include <algorithm>
using namespace std;
void bubbleSort(vector<int>& v)
{
int temp;
for (int k = 1; k < v.size(); ++k)
{
int flag = 0; //to check if the pass actually swaps any elements
for (int i = 0; i < (v.size() - k); ++i)//check only till the unsorted part of vector
{
if (v[i]>v[i + 1]) //check if adjacent is greater
{
temp = v[i];
v[i] = v[i + 1];
v[i + 1] = temp;
flag = 1;
}
}
if (flag == 0) //no swap means already sorted.
break;
}
}
int main()
{
srand(time(0));
vector<int> v;
int insert;
for (int i = 0; i < 10; ++i)//randomly generate and fill the vector with unique numbers
{
insert = rand() % 15;
if (find(v.begin(),v.end(),insert)==v.end())
v.push_back(insert);
}
cout << "Before sort: ";
for (auto i : v)
cout << i << " ";
bubbleSort(v);
cout << endl;
cout << "After sort: ";
for (auto i : v)
cout << i << " ";
system("pause");
}