0

Why does it generate a random number in the sequence of iterations when outputting the buble sort algorithm?

void bublesort(double array[n]){ for(int i = 0; i < n; i++) { for(int j = 1; j < n-i+1;j++) if (array[j-1] > array[j]) { double temp = array[j-1]; array[j-1] = array[j]; array[j] = temp; } cout << "Iteracioni " << i+1 << endl; for(int i = 0; i <= n; i++) cout << array[i] << endl; }

18th Dec 2017, 12:39 PM
Emanuel Dajti
Emanuel Dajti - avatar
1 Respuesta
0
In your last for loop, the condition must not be including n. Use less than instead of less than or equal to. That's probably gonna solve it. A question pops up in my mind here! Don't you get index out of range error? 🤔 try this one below 👇 for(int i = 0; i < n; i++)
18th Dec 2017, 8:13 PM
Hamidreza
Hamidreza - avatar