forked from kelvins/algorithms-and-data-structures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BubbleSort.cpp
47 lines (43 loc) · 991 Bytes
/
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
#include <iostream>
#include <vector>
using namespace std;
vector<int> bubbleSort(vector<int> vector)
{
for (uint32_t end = vector.size()-1; end > 0; --end)
{
for (uint32_t index = 0; index < end; ++index)
{
if (vector[index] > vector[index+1])
{
int temp = vector[index];
vector[index] = vector[index+1];
vector[index+1] = temp;
}
}
}
return vector;
}
void showVector(vector<int> vector)
{
for (uint32_t i = 0; i < vector.size(); ++i)
{
if (i +1 == vector.size())
cout << vector[i];
else
cout << vector[i] << ", ";
}
cout << "\n";
}
int main()
{
vector<int> vector;
for (uint32_t i = 0; i < 10; ++i)
{
vector.push_back(rand() % 100);
}
cout << "Initial Vector: ";
showVector(vector);
vector = bubbleSort(vector);
cout << "Sorted Vector: ";
showVector(vector);
}