Say we have a set of numbers like this:
values = (1..10000).to_a.sample(5000)
There are 5000 of them and they're in a random order. Let's implement the Merge Sort algorithm to get them sorted.
Find the wikipedia description of Merge Sort here. You'll want to pay attention to the animated GIF that illustrates the concept. There's also this sweet line dance.
Implement your own version of the algorithm. Avoid the temptation to Google solutions in Ruby or other languages. Use just the information linked above and you're own deep thinking.
Let's do some analysis of the algorithm. Continue with the idea that comparing two numbers is the only instruction that is "expensive" in this algorithm. Say you're going to sort a set of just 10 numbers.
- What would the best case input be for this algorithm?
- How many comparisons would run in the best-case scenario?
- What would the worst case input be for this algorithm?
- How many comparisons would run in the worse-case scenario?
- What do those results tell you about the relative merits of Merge Sort as compared with something simpler like a Bubble Sort?