Sign in to follow this  
gflores

Efficient way to sort only three values?

Recommended Posts

gflores    127
I know of several sorting methods, but my professor, in assigning the project, implied that there is an easy way to sort only three values, like a shortcut or whatnot. I know how I would do it using a bubble sort, but not of a better, more efficient way. This would only be for three values (not more or less). Any ideas? Thanks in advance. BTW, the general flow of the program is this (and can't be changed).
Quote:
sort(int &min, int &mid, int &max) { // sort and restore the values } int main() { //ask user for three values cin >> min >> mid >> max; sort(min,mid,max); }

Share this post


Link to post
Share on other sites
Fruny    1658
Gamedev is not the place to ask help on school assignments. Ask your professor or TA instead.

Sorting 3 elements can be done with two or three comparisons, depending on the actual values. Working it out by hand isn't difficult.

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.
Sign in to follow this