|
3 | 3 |
|
4 | 4 | using namespace std;
|
5 | 5 |
|
6 |
| -vector<int> bubbleSort(vector<int> vetor) |
| 6 | +vector<int> bubbleSort(vector<int> vector) |
7 | 7 | {
|
8 |
| - for (uint32_t fim = vetor.size()-1; fim > 0; --fim) |
| 8 | + for (uint32_t end = vector.size()-1; end > 0; --end) |
9 | 9 | {
|
10 |
| - for (uint32_t indice = 0; indice < fim; ++indice) |
| 10 | + for (uint32_t index = 0; index < end; ++index) |
11 | 11 | {
|
12 |
| - if (vetor[indice] > vetor[indice+1]) |
| 12 | + if (vector[index] > vector[index+1]) |
13 | 13 | {
|
14 |
| - int troca = vetor[indice]; |
15 |
| - vetor[indice] = vetor[indice+1]; |
16 |
| - vetor[indice+1] = troca; |
| 14 | + int temp = vector[index]; |
| 15 | + vector[index] = vector[index+1]; |
| 16 | + vector[index+1] = temp; |
17 | 17 | }
|
18 | 18 | }
|
19 | 19 | }
|
20 |
| - return vetor; |
| 20 | + return vector; |
21 | 21 | }
|
22 | 22 |
|
23 |
| -void showVector(vector<int> vetor) |
| 23 | +void showVector(vector<int> vector) |
24 | 24 | {
|
25 |
| - for (uint32_t i = 0; i < vetor.size(); ++i) |
| 25 | + for (uint32_t i = 0; i < vector.size(); ++i) |
26 | 26 | {
|
27 |
| - cout << vetor[i] << ", "; |
| 27 | + if (i +1 == vector.size()) |
| 28 | + cout << vector[i]; |
| 29 | + else |
| 30 | + cout << vector[i] << ", "; |
28 | 31 | }
|
29 | 32 | cout << "\n";
|
30 | 33 | }
|
31 | 34 |
|
32 | 35 | int main()
|
33 | 36 | {
|
34 |
| - vector<int> vetor; |
| 37 | + vector<int> vector; |
35 | 38 | for (uint32_t i = 0; i < 10; ++i)
|
36 | 39 | {
|
37 |
| - vetor.push_back(rand() % 100); |
| 40 | + vector.push_back(rand() % 100); |
38 | 41 | }
|
39 |
| - showVector(vetor); |
40 |
| - vetor = bubbleSort(vetor); |
41 |
| - showVector(vetor); |
| 42 | + cout << "Initial Vector: "; |
| 43 | + showVector(vector); |
| 44 | + vector = bubbleSort(vector); |
| 45 | + cout << "Sorted Vector: "; |
| 46 | + showVector(vector); |
42 | 47 | }
|
0 commit comments