Hi guys,
I'm learning C++ and one of the exercises is to create a program that sorts an unsorted array. So I created a prog, but it won't work, only the first 2 numbers and the last 2 were sorted, the rest remained to it's original position.
So I took a look at my code, rewrote it 2 times and still it has the same issue! Can somebody please help me out?
Code:#include <iostream> using namespace std; int main() { int n[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; int i, j, p; i = 0; j = 0; p = 0; // the unsorted list for (i = 0; i < 10; i++){ cout << "[" << i << "] = "; cin >> n[i]; } i = 0; j = 0; cout << endl; cout << "Unsorted list = "; for (i = 0; i < 10; i++) cout << n[i] << ", "; cout << endl; cout << "Sorting..." << endl; // sorting... i = 0; while (true){ if (n[i] < n[i + 1]){ ++i; if (i = 8) break; } if (n[i] > n[i + 1]){ // swap -------- p = n[i + 1]; n[i + 1] = n[i]; n[i] = p; //-------------- i = 0; } if (n[i] == n[i + 1]) ++i; } // the sorted list cout << "Sorted list = "; for (i = 0; i < 10; i++) cout << n[i] << ", "; }



LinkBack URL
About LinkBacks


