Skip to content

Commit 8a61512

Browse files
committed
Translated BubbleSort Algo in cpp and addded a few couts in the output for clarify
1 parent 8b245e8 commit 8a61512

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed

src/cpp/BubbleSort.cpp

+21-16
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,45 @@
33

44
using namespace std;
55

6-
vector<int> bubbleSort(vector<int> vetor)
6+
vector<int> bubbleSort(vector<int> vector)
77
{
8-
for (uint32_t fim = vetor.size()-1; fim > 0; --fim)
8+
for (uint32_t end = vector.size()-1; end > 0; --end)
99
{
10-
for (uint32_t indice = 0; indice < fim; ++indice)
10+
for (uint32_t index = 0; index < end; ++index)
1111
{
12-
if (vetor[indice] > vetor[indice+1])
12+
if (vector[index] > vector[index+1])
1313
{
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;
1717
}
1818
}
1919
}
20-
return vetor;
20+
return vector;
2121
}
2222

23-
void showVector(vector<int> vetor)
23+
void showVector(vector<int> vector)
2424
{
25-
for (uint32_t i = 0; i < vetor.size(); ++i)
25+
for (uint32_t i = 0; i < vector.size(); ++i)
2626
{
27-
cout << vetor[i] << ", ";
27+
if (i +1 == vector.size())
28+
cout << vector[i];
29+
else
30+
cout << vector[i] << ", ";
2831
}
2932
cout << "\n";
3033
}
3134

3235
int main()
3336
{
34-
vector<int> vetor;
37+
vector<int> vector;
3538
for (uint32_t i = 0; i < 10; ++i)
3639
{
37-
vetor.push_back(rand() % 100);
40+
vector.push_back(rand() % 100);
3841
}
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);
4247
}

0 commit comments

Comments
 (0)