Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^5: move all 0s in an array to the beginning keeping other elements order same

by Laurent_R (Canon)
on May 05, 2014 at 06:09 UTC ( [id://1085001]=note: print w/replies, xml ) Need Help??


in reply to Re^4: move all 0s in an array to the beginning keeping other elements order same
in thread move all 0s in an array to the beginning keeping other elements order same

Thank you, Dave, for your answer, but I think that the merge sort algorithm is inherently stable, independently of its specific implementations. But you are probably right that it is better not to take any cnance and make sure that stability is guaranteed.
  • Comment on Re^5: move all 0s in an array to the beginning keeping other elements order same

Replies are listed 'Best First'.
Re^6: move all 0s in an array to the beginning keeping other elements order same
by davido (Cardinal) on May 05, 2014 at 06:35 UTC

    What I didn't state clearly in my previous comment is this:

    In the old days, Perl's sort used a QuickSort algorithm, and it was swapped out, replaced by a Merge Sort starting (if my memory serves) with Perl 5.8. This swap in the implementation of Perl's "sort" was done primarily to avoid the potential for QuickSort to go quadratic on some inputs. A secondary effect was that it provided a stable sort.

    However unlikely it is that Perl would ever switch again, and this time to an unstable sort, the fact that modern Perls have a stable sort is still just an implementation detail. By specifying "use sort 'stable'", you are making a declaration of what semantics you require, leaving nothing to chance. Additionally, it signals to a future maintainer that this code would not fly if deprived of a stable sort.


    Dave

Re^6: move all 0s in an array to the beginning keeping other elements order same
by ikegami (Patriarch) on May 31, 2014 at 04:58 UTC

    [ Sorry, missed your reply. ]

    but I think that the merge sort algorithm is inherently stable

    But sort doesn't necessarily use merge sort. While it's currently the default, it hasn't always been, and it might not always be.

      Thank you ikegami for your answer.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1085001]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2024-04-26 00:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found