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.
Show HN: Write a sort, watch it go
21–30 of 89 posts
Re: Show HN: Write a sort, watch it go
#22The 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
#23The 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
#24Re: Show HN: Write a sort, watch it go
#25The 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.
VA.swap(left, Math.floor(Math.random() * (right - left + 1)) + left)
Re: Show HN: Write a sort, watch it go
#26I 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?
VA = [4, 2, 5, 7]
for y in VA
setTimeout('console.log(' + y + ')', y*1000)Re: Show HN: Write a sort, watch it go
#27>Nothing will display if there is an infinite loop. Did someone solve the halting problem while I wasn't looking?
Re: Show HN: Write a sort, watch it go
#28I 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?
VA = [4, 2, 5, 7]
for y in VA
setTimeout "console.log(#{y})", y*1000Re: Show HN: Write a sort, watch it go
#29The 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)
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)Re: Show HN: Write a sort, watch it go
#30This Quick Sort Visualization is cool too. Were you inspired by this? ;-) http://www.youtube.com/watch?v=ywWBy6J5gz8