cboPerl has asked for the wisdom of the Perl Monks concerning the following question:
It creates 20 random numbers. Please, could someone help to insert a code for organizing the random numbers from smallest to largest without using sort!!! Thanks#!/usr/bin/perl -w use strict; my $RandNums = ""; for(my $i = 0; $i < 20; $i++){ $RandNums = $RandNums.','.rand(20); } my @array = ($RandNums); print @array;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: random #s
by GrandFather (Saint) on Oct 07, 2016 at 03:23 UTC | |
That code doesn't do what you think it does. Your array has only one element, a string containing twenty numbers. If you print $RandNums instead of @array you will see that you get the same result. A Perlish way to populate your array is:
See the Wikipedia Sorting_algorithm article for different ways you might handle the sorting. If we just give you the code you won't learn much and your teacher won't be happy.
Premature optimization is the root of all job security
| [reply] [d/l] |
by cboPerl (Initiate) on Oct 07, 2016 at 03:51 UTC | |
What it does :
| [reply] [d/l] [select] |
by NetWallah (Canon) on Oct 07, 2016 at 04:19 UTC | |
Since you have numbers, use the "<=>" operator instead. ALso, please use <code/> tags. Output:
...it is unhealthy to remain near things that are in the process of blowing up. man page for WARP, by Larry Wall | [reply] [d/l] [select] |
by choroba (Cardinal) on Oct 07, 2016 at 08:37 UTC | |
by davido (Cardinal) on Oct 07, 2016 at 15:58 UTC | |
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 | [reply] |
by hippo (Archbishop) on Oct 07, 2016 at 16:31 UTC | |
by Ratazong (Monsignor) on Oct 07, 2016 at 19:36 UTC | |
by GrandFather (Saint) on Oct 07, 2016 at 04:07 UTC | |
I can't easily read that because you can't be bothered to use code tags around your code. However you might look up the difference between cmp and <=>.
Premature optimization is the root of all job security
| [reply] |
|
Re: random #s
by BrowserUk (Patriarch) on Oct 07, 2016 at 17:02 UTC | |
Why sort at all? Generate your random numbers already ordered:
With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] [d/l] |
by davido (Cardinal) on Oct 07, 2016 at 17:30 UTC | |
I think this exhibits a pretty strong bias toward the lower numbers generated, and against the higher numbers. It may be pseudo random but the distribution is not close to flat.
...which produces...
...and that is with adding the filtering-out of undefined values that found their way into @orderedRands. I hope my adaptation of your algorithm didn't miss an important nuance. Dave | [reply] [d/l] [select] |
by BrowserUk (Patriarch) on Oct 07, 2016 at 21:04 UTC | |
this exhibits a pretty strong bias toward the lower numbers generated, and against the higher numbers. Yes. A side-effect of my lazy way of attempting to ensure that at least 20 numbers are produced each time. With 400 inputs to choose from the selector value should be 0.05 not 0.075; but the nature of random is that whilst 0.05 produces a fair pick: <Reveal this spoiler or all in this thread>
It will on occasion produce as many as 44 values or as few as 3:
By raising the selector value to 0.075, I made it far more likely that it would produce at least 20 values. The list slice ensures that it is not more than 20; but also introduces the bias by always throwing away the higher values when it overproduces. The following results from the above code with the only change 0.05 => 0.075; demonstrates that the distribution of the range is still very fair; but on average 50% more numbers are produced each time before the slice operation trims the numbers back, (and introduces the bias). It also shows that the probability of under-producing is greatly lessened:
This could be fixed by repeating the process until exactly 20 numbers come out, which ensure the fairness:
Of course that is far more expensive than doing the sort that it avoids. But then, my post was nothing more than a semi-humorous response to a question that itself is something of a joke. With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] [d/l] [select] |
by tybalt89 (Monsignor) on Oct 07, 2016 at 19:45 UTC | |
The algorithm needs a minor tweak:
...which produces...
Which looks pretty good. | [reply] [d/l] [select] |
|
Re: random #s
by tybalt89 (Monsignor) on Oct 07, 2016 at 16:35 UTC | |
hehehe
| [reply] [d/l] |