Live data from Hacker News

Show HN: Write a sort, watch it go

visualsort.appspot.com

21–30 of 89 posts

Re: Show HN: Write a sort, watch it go

#21

The 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.

Poor choice of the pivot makes the implementation bad, but absolutely not incorrect.

Re: Show HN: Write a sort, watch it go

#22

The 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.

Re: Show HN: Write a sort, watch it go

#23

The 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.

[deleted]

Re: Show HN: Write a sort, watch it go

#25

The 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.

Add the following right above the line "pivot = left" to fix it:

VA.swap(left, Math.floor(Math.random() * (right - left + 1)) + left)

Re: Show HN: Write a sort, watch it go

#26

I wanted to implement the sleep sort algorithm, but I can't use VA = [4, 2, 5, 7]; for(x=0; x it tells me I can't use "var". And when I remove that I get some syntax error. Any nice alternative way to implement a sleep sort in coffescript?

This is the coffeescript equivalent, not sure how to get it working in the visualisation though as it doesn't involve swaps.

  VA = [4, 2, 5, 7]
  for y in VA
    setTimeout('console.log(' + y + ')', y*1000)

Re: Show HN: Write a sort, watch it go

#28

I wanted to implement the sleep sort algorithm, but I can't use VA = [4, 2, 5, 7]; for(x=0; x it tells me I can't use "var". And when I remove that I get some syntax error. Any nice alternative way to implement a sleep sort in coffescript?

Try this:

    VA = [4, 2, 5, 7]
    for y in VA
      setTimeout "console.log(#{y})", y*1000

Re: Show HN: Write a sort, watch it go

#29

The 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.

Add the following right above the line "pivot = left" to fix it: VA.swap(left, Math.floor(Math.random() * (right - left + 1)) + left)

Alternatively you can insert this code for the median of 3 version:

   centre = (left + right) / 2;
   if VA.lt(left, centre)
     if VA.lt(centre, right)
       VA.swap(left, centre)
     else if VA.lt(left, right)
       VA.swap(left, right)
   else
     if VA.lt(centre, right)
       VA.swap(left, centre)
     else if VA.lt(left, right)
       VA.swap(left, right)
Post reply on HN