forked from kelvins/algorithms-and-data-structures
-
Notifications
You must be signed in to change notification settings - Fork 1
/
BubbleSort.cpp
42 lines (38 loc) · 838 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
#include <iostream>
#include <vector>
using namespace std;
vector<int> bubbleSort(vector<int> vetor)
{
for (uint32_t fim = vetor.size()-1; fim > 0; --fim)
{
for (uint32_t indice = 0; indice < fim; ++indice)
{
if (vetor[indice] > vetor[indice+1])
{
int troca = vetor[indice];
vetor[indice] = vetor[indice+1];
vetor[indice+1] = troca;
}
}
}
return vetor;
}
void showVector(vector<int> vetor)
{
for (uint32_t i = 0; i < vetor.size(); ++i)
{
cout << vetor[i] << ", ";
}
cout << "\n";
}
int main()
{
vector<int> vetor;
for (uint32_t i = 0; i < 10; ++i)
{
vetor.push_back(rand() % 100);
}
showVector(vetor);
vetor = bubbleSort(vetor);
showVector(vetor);
}