QuickSort conclusions (Off-Topic)

by marmot 1333 @, Wednesday, October 23, 2013, 08:58 (4055 days ago)

There was some interest in my Java-based Quicksort project but that thread got locked.

To update: I was using an algorithm out of my book that used the right-most value as pivot. This led to the partitions being extremely unbalanced (8191 vs 1 in case of 2^13) and led to stack overflows starting at 2^13.

So I just updated my algorithm to utilize randomized quicksort; it chooses a pseudo-random element in the array and uses that as pivot. This solved my stack overflows as I was able to sort up to 2^20 for sorted, reverse sorted, and shuffled arrays with no problem.

So I guess I was lucky, Sekhmet, because my quicksort was performing faster than heapsort on the largest set (2^20). Here's some sample output.


Starting a HeapSort with n sorted numbers, where n = 1048576
Time to sort 1048576 sorted entries with Heapsort was 1015 milliseconds.
[(1): 1]
[(524288): 524288]
[(1048576): 1048576]
...
Starting a HeapSort with n reversed numbers, where n = 1048576
Time to sort 1048576 reversed entries with Heapsort was 886 milliseconds.
[(1): 1]
[(524288): 524288]
[(1048576): 1048576]
...
Starting a HeapSort with n shuffled numbers, where n = 1048576
Time to sort 1048576 shuffled entries with Heapsort was 1483 milliseconds.
[(1): 1]
[(524288): 524288]
[(1048576): 1048576]
...
Before sorting the array is:
[(1): 1]
[(524288): 524288]
[(1048576): 1048576]
Starting a QuickSort with n sorted numbers, where n = 1048576
Time to sort 1048576 sorted entries with QuickSort was 396 milliseconds.
After sorting the array is:
[(1): 1]
[(524288): 524288]
[(1048576): 1048576]
...
Before sorting the array is:
[(1): 1048576]
[(524288): 524289]
[(1048576): 1]
Starting a QuickSort with n reversed numbers, where n = 1048576
Time to sort 1048576 reversed entries with QuickSort was 400 milliseconds.
After sorting the array is:
[(1): 1]
[(524288): 524288]
[(1048576): 1048576]
...
Before sorting the array is:
[(1): 857704]
[(524288): 190051]
[(1048576): 792221]
Starting a QuickSort with n shuffled numbers, where n = 1048576
Time to sort 1048576 shuffled entries with QuickSort was 481 milliseconds.
After sorting the array is:
[(1): 1]
[(524288): 524288]
[(1048576): 1048576]

It's like he's trying to tell me something!

by Oholiab @, Wednesday, October 23, 2013, 12:06 (4055 days ago) @ marmot 1333

I have absolutely no clue about which you speak. Further evidence that most people are way smarter than I am.

Avatar

QuickSort conclusions

by GrizzNKev, Down the street from Microsoft, Wednesday, October 23, 2013, 13:18 (4055 days ago) @ marmot 1333

I'm not into any complex sorting algorithms yet, but I did successfully complete the IPC assignment I mentioned in that thread with few issues. Now I know how to effectively share memory and communicate between processes to create a distributed workload. Yay!

QuickSort conclusions

by Samusaaron3, Bay Area, Wednesday, October 23, 2013, 13:31 (4055 days ago) @ marmot 1333
edited by Samusaaron3, Wednesday, October 23, 2013, 13:40

You should experiment using the median-of-three method for choosing a pivot, and see how much faster you might be able to make it go :)

Very cool to see your results!

Avatar

I Vaguely Remember Doing This

by Blackt1g3r @, Login is from an untrusted domain in MN, Wednesday, October 23, 2013, 14:23 (4055 days ago) @ marmot 1333

This is probably the last time you'll ever write these sorting algorithms, enjoy it while it lasts. :)

QuickSort conclusions

by marmot 1333 @, Wednesday, October 23, 2013, 14:27 (4055 days ago) @ Samusaaron3

Cool, I think I will.

How do you choose the three? Beginning, middle, end? Three randoms?

QuickSort conclusion

by Samusaaron3, Bay Area, Wednesday, October 23, 2013, 16:04 (4055 days ago) @ marmot 1333
edited by Samusaaron3, Wednesday, October 23, 2013, 16:12

Three randoms should work well. If you want to go all out, you could also implement switching to insertion sort once you're dealing with small arrays (around size 10 or so).

You can read up on it here: http://algs4.cs.princeton.edu/23quicksort/

Let me know if the median pivot works well!

Avatar

Yah, just use the libraries...

by RC ⌂, UK, Wednesday, October 23, 2013, 16:18 (4055 days ago) @ Blackt1g3r

A wise man once told me:

"A good programmer knows how to write good code,
but a great programmer knows how to steal good code."

;)

Avatar

I Vaguely Remember Doing This

by Schooly D, TSD Gaming Condo, TX, Saturday, October 26, 2013, 08:52 (4052 days ago) @ Blackt1g3r

This is probably the last time you'll ever write these sorting algorithms

Unless he decides to interview for a programming job within 5 years after graduation.

Avatar

Agreed

by kidtsunami @, Atlanta, GA, Saturday, October 26, 2013, 11:18 (4052 days ago) @ Schooly D

Insomniac definitely had a pretty tricky in place array sort in their code test.

Back to the forum index
RSS Feed of thread