Below is a function that can be used to sort different number in a descending or
ID: 3531769 • Letter: B
Question
Below is a function that can be used to sort different number in a descending order. Thereare120differentpermutationsoffiveunequalvalues,whichwouldmakeittedioustoexhaustivelytestallpossibleinputs.So Whatpre-conditionmustbetruejustbeforeyourlastcalltosortThreetoguaranteeallnumbersaresortedattheend?Whatmustbetruebeforethethirdcalltoguaranteethat?(etc.)
HINT:Feelfreetosimplifyyourassertionswithclaimslikethis:
(A,B)>(C,D,E);thatis,BothAandBaregreaterthanalltherest
Thisismuchsimplerthan(A>C&&A>D&&A>E&&B>C&&B>D&&B>E)
void sortThree(int &first, int &second, int &third)
{
if(first<second)
swap(first, second);
if(first<third)
swap(first, third);
if(second<third)
swap(second, third);
}
void sortFive (a, b, c, d, e)
{
sortThree(a,b,c);
sortThree(a,d,e);
sortThree(b,c,d);
sortThree(c,d,e);
}
Explanation / Answer
http://www.miniwebtool.com/sort-numbers/
http://www.mathworks.in/help/matlab/ref/sort.html
http://www.ehelp.com/questions/10433823/how-to-sort-in-descending-order-after-generating-random-numbers-between-0-99
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.