Overclock.net banner

1 - 4 of 4 Posts

·
Registered
Joined
·
863 Posts
Discussion Starter #1
For my homework I have a list of data, v1, v2,v3. I need to sort these in ascending order using void sort. However, the handouts my professor gave me, and my book, make it hard to understand just how I have to set up the array. If anyone could help me grasp how to set it up I would be most thankful.

void sort (double& v1, double& v2, double& v3);
//POST: reorder values such that v1<=v2<=v3
 

·
Registered
Joined
·
579 Posts
do you have to do it for 3 values or more?

if you only need to sort 3 values then check each of them

if v1 > v2
swap v1 with v2
if v1 > v3
swap v1 with v2
if v2 > v3
swap v2 with v3

swap the variable using a third variable
buffer = v3;
v3 = v2;
v2 = buffer;
/
 

·
Registered
Joined
·
863 Posts
Discussion Starter #3
So all I need to do is use a swap function? I was under the impression I needed to use an array. This makes it much easier
 

·
Registered
Joined
·
579 Posts
if instead of sort(double& v1,double& v2 etc...) you had sort(int v1[5]) you could make a loop that does basically the same thing but for the whole array but it would get messy since you would need to copy the arrays contents to another array in order to insert your variable it would be better to use vectors in that case since they have a insert function.
 
1 - 4 of 4 Posts
Top