MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today**New to MATLAB?**

Asked by Isaac Reed
on 2 May 2013

Write a Matlab function that would arrange a series of numbers (i.e. a row vector) in a descending order. As an example, use the vector 2 15 50 3 8 11 5 29

how would i do this without using the sort cmd?

thanks in advance.

*No products are associated with this question.*

Answer by Walter Roberson
on 2 May 2013

https://en.wikipedia.org/wiki/Sorting_algorithm

Cedric Wannaz
on 2 May 2013

Illustrated below if you have a lot of time.. ;)

`Bubble-sort :`http://www.youtube.com/watch?v=lyZQPjUT5B4`Insert-sort :`http://www.youtube.com/watch?v=ROalU379l3U`Merge-sort :`http://www.youtube.com/watch?v=XaqR3G_NVoo`Quick-sort :`http://www.youtube.com/watch?v=kDgvnbUIqT4`Select-sort :`http://www.youtube.com/watch?v=Ns4TPTC8whw`Shell-sort :`http://www.youtube.com/watch?v=CmPA7zE8mx0

Answer by Jan Simon
on 2 May 2013

Edited by Jan Simon
on 2 May 2013

A bold version - hesitate to submit it except you can explain the "infinite monkey theorem" fluently:

x = [2, 15, 50, 3, 8, 11, 5, 29]; n = length(x); while ~issorted(x) x = x(randperm(n)); end

Beside the fact, that this requires a ZillionHz CPU to get finished in a bearable amount of time for larger input, it is short and clear. Unfortunately there is no guarantee, that this algorithm stops, such that it does not satisfy a strict definition of a program. But the question text did not demand a "program", but a "function". If you are able to explain the the difference from the viewpoint of coding theory, I'd accept this as a valid solution.

## 1 Comment

## Jan Simon (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/74299#comment_146757

It is a good and established method for homework questions to show, what you have done so far and explain, which problems occurred. You cannot and should not expect the forum to solve your homework.