Is this some sort of joke?

The algorithm you settled upon is known as a Bozosort, and is one of the algorithms studied in the infamous paper, Sorting the Slow Way: An Analysis of Perversely Awful Randomized Sorting Algorithms. That's right -- the algorithm that involves swapping random elements until the list is in order is considered not just awful, but perversely awful. "Perverse: showing a deliberate and obstinate desire to behave in a way that is unreasonable or unacceptable, often in spite of the consequences." .... that Perversely Awful -- So bad that one must be intentionally selecting the algorithm for its awfulness.

In that paper, this algorithm is shown to have an average run-time of O(n!), so in the case of your 20 numbers, on average it will take 432902008176640000 iterations to achieve a sorted solution. In the worst case it might never finish, since the solution is left to chance. In practical terms, as iterations increases toward infinity the probability of never finding a solution declines toward zero, meaning that if you are given infinite time a solution is virtually guaranteed. Who has infinite time?

If you are looking for an algorithm that is pretty simple to reason about, search for Bubble Sort. It's not terribly efficient, but it is also not perversely awful, and tends to be one of the easier sorting algorithms to comprehend and commit to code.


Dave


In reply to Re^3: random #s by davido
in thread random #s by cboPerl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.