>Nothing will display if there is an infinite loop. Did someone solve the halting problem while I wasn't looking?
I assume it just computes every step, and then displays them all.
Show HN: Write a sort, watch it go
31–40 of 89 posts
Re: Show HN: Write a sort, watch it go
#32Needs a shuffle() function - can't bogosort without it.
Re: Show HN: Write a sort, watch it go
#33Re: Show HN: Write a sort, watch it go
#34The quicksort implementation is incorrect. The code simply picking the left-most element as the pivot, and as a result it becomes pathologically slow for nearly-sorted array -- for example, run bubblesort then quicksort, or run quicksort twice -- not to mention potential stack overflow problem due to O(N) recursive call.
Re: Show HN: Write a sort, watch it go
#35 pos = 1
while pos 1)
pos--
else
pos++Re: Show HN: Write a sort, watch it go
#36This allows "algorithms" like this:
for x in [0...VA.length]
while (VA.get(x) > x)
VA.swap(VA.get(x), x)Re: Show HN: Write a sort, watch it go
#37I'm not sure if this possible or relevant to record based on how Javascript engines compile and work, but it would seem interesting to know.
Re: Show HN: Write a sort, watch it go
#38The input set is problematic because it contains every number from 0 to VA.length exactly once. This allows "algorithms" like this: for x in [0...VA.length] while (VA.get(x) > x) VA.swap(VA.get(x), x)
Also, that's not going to work quite right - the values are [1..VA.length] (see the bottom of the page), while the indices are [0...VA.length].
Re: Show HN: Write a sort, watch it go
#39Re: Show HN: Write a sort, watch it go
#40The quicksort implementation is incorrect. The code simply picking the left-most element as the pivot, and as a result it becomes pathologically slow for nearly-sorted array -- for example, run bubblesort then quicksort, or run quicksort twice -- not to mention potential stack overflow problem due to O(N) recursive call.
That's still a quicksort - it always has n^2 as its worst case I thought? If you pick the wrong pivot each time.
Some others have posted methods (random pivot, median of 3). The gnu libc qsort implementation is a good learning tool (it uses median of 3). I think CLRS has the guaranteed n log n version.