Skip to content

Latest commit

 

History

History
44 lines (31 loc) · 3.93 KB

File metadata and controls

44 lines (31 loc) · 3.93 KB

Searching & Sorting

Sorting Algorithm Animations

Sorting Videos

Sorting algorithm comparison

This Google Sheets document contains a comparison of the 5 sorting algorithms we covered using the code from the jsjf package from Java Foundations. It includes runs of different sizes and different types of arrays and the differences in the number of comparisons performed and the elapsed time. (Note that elapsed time is not 100% accurate, so don't get too hung up on any one particular result.)

A few questions to think about:

  • It should make sense why insertion sort performs so well on sorted data (and why that isn't particularly impressive!). But why is its performance also consistently better on random arrays?
  • Why is merge sort so much slower than quick sort (elapsed time) despite usually doing fewer comparisons.
  • 1,0002 = 1,000,000 and 1000 * log2 1,000 ~= 10,000. But the O(n2) algorithms do <500,000 comparisons and the O(n log n) algorithms do between 5,000 and 14,000 comparisons. Why the discrepancy?
  • Why is quicksort the worst algorithm to sort the Random3 array where size=10?
  • Looking at the data, can you say which algorithm is "best"?
  • Trick question: What is the first "sorting algorithm" we implemented in this course?

Interesting links

More information about shuffling

We discussed in the notes a shuffling algorithm that used sorting. If you are interested in other techniques of shuffling, the following links may be interesting to you. Keep in mind that for the sorting project you must use the algorithm described in the notes, not the ones described the links below!