Hi all,
I am trying to get ExchangeSort to output numbers from an array to be in increasing order, yet the results come out in decreasing order, or as I put it, in reverse. Can anyone tell me how to get the results in increasing order?
Code:
#include<iostream>
using namespace std;
int main(void)
{
int array[5]; // An array of integers.
int length = 5; // Length of the array.
int i, j;
int temp;
//Some input
for (i = 0; i < 5; i++)
{
cout << "Enter a number: ";
cin >> array[i];
}
//Algorithm
for(i = 0; i < (length -1); i++)
{
for (j=(i + 1); j < length; j++)
{
if (array[i] < array[j])
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
//Some output
for (i = 0; i < 5; i++)
{
cout << array[i] << endl;
}
}
Example of input: (in order from first to last) 7, 3, 5, 6, 2
The result ends up as 7,6,5,3,2 instead of what should be 2,3,5,6,7. Any help would be appreciated. Thanks.