-
Notifications
You must be signed in to change notification settings - Fork 265
/
Copy pathBubbleSort.cpp
39 lines (35 loc) · 872 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
#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);
}