Gente estou lendo livro "Programming principles and practice using c++ (2nd edition)" do criador do C++. Tem um exercicio la bem no começo falando pra fazer um programa que ordena os numeros dados. Eu fiz usando BubbleSort, queimei bastante neurônios fazendo mais acho que consegui. Queria saber se ele tem algum erro ou alguma situação que de errado, eu fiz alguns testes e parece funcionar. Obrigado.
Código abaixo.
#include <iostream>
using namespace std;
void bubbleSort(int * my_integer_array_pointer /*pointer to original array*/,unsigned int array_size)
{
int tempChanger = 0;
unsigned int Swaps = 0;
unsigned int i = 0;
for (i = 0; i < (array_size -1); i++) //iterate "my_integer_array_pointer", "array_size-1" times
{
if (*(my_integer_array_pointer+i) > *(my_integer_array_pointer+i+1)) // if left number is greater than right number, swapp then
{
tempChanger = *(my_integer_array_pointer+i);
*(my_integer_array_pointer+i) = *(my_integer_array_pointer+i+1);
*(my_integer_array_pointer+i+1) = tempChanger;
Swaps++; //count the number of swapped numbers
}
}
if (Swaps > 0) // if swap more than 0 times, need to iterate list again
{
bubbleSort(my_integer_array_pointer,array_size);
}
/*
else = list is already sorted, stop function.
*/
}
int main()
{
cout << "Please type the amount of numbers you want to sort:" << endl;
int my_integer_array_size;
cin >> my_integer_array_size;
int my_integer_array[my_integer_array_size] = {};
for (int i = 0; i < my_integer_array_size; i++) //iterator used to assign a value to each element of "my_integer_array" with "cin"
{
cout << "Please type the " << i+1 << " number" << endl;
cin >> my_integer_array[i];
}
cout << endl;
bubbleSort(my_integer_array,my_integer_array_size);
cout << "The ordered array is below :" << endl;
for (unsigned int i = 0; i < sizeof(my_integer_array) / sizeof(int); i++) //iterator to show the results
{
cout << my_integer_array[i] << endl;
}
return 0;
}