Hi all, I'm having a bit of trouble sorting my array into ascending order. I don't want someone to write the code for me, but if I could get a hefty nudge in the right direction for the appropriate logic or algorithm, that would be a great help.
So far, I've created a function that will swap low/high values, and it works for the first two cells of the array, but how do I get it to carry through the rest of the array? I know it'll have to loop, but would it be a for or while loop? Would it be nested loops? And how can I set a condition on something like that?
My function(s) for switching the elements reads as follows, with the arguments by reference to indexes of the array:
void sort_array(double &a, double &b)
{
if (b < a)
my_swap(b, a);
}
void my_swap(double &x, double &y)
{
double holdy=y;
y = x;
x = holdy;
}
|
Do I have to run the sort_array function (x) times, checking the entire array each time until all the elements are sorted out? Should I rewrite the function to use an array as an argument?
Alternatively, could I instead write a function to find the lowest value in the array, swap it with array[0], and then start searching for the lowest value from array[1] to the end of the array and swap that value with array[1]... etc?
Is there a simpler way of going about this?
Will I have any hair left in my head when this assignment is done?
Any help y'all can maybe toss my way would be much appreciated. Thank you!
~pH