About bubble sort vs merge sort

algorithm, sorting

Solution

It's a trick question. If you just want the maximum, (or indeed, the kth value for any k, which includes finding the median), there's a perfectly good `O(n)` algorithm. Sorting is a waste of time. That's what they want to hear.

As you say, the algorithm for maximum is really trivial. To ace a question like this, you should have the quick-select algorithm ready, and also be able to suggest a heap datastructure in case you need to be able to mutate the list of values and always be able to produce the maximum rapidly.

Problem

This is an interview question that I recently found on Internet: If you are going to implement a function which takes an integer array as input and returns the maximum, would you use bubble sort or merge sort to implement this function? What if the array size is less than 1000? What if it is greater than 1000? This is how I think about it: First, it is really weird to use sorting to implement the above function. You can just go through the array once and find the max one. Second, if have to make a choice between the two, then bubble sort is better - you don't have to implement the whole bubble sort procedure but only need to do the first pass. It is better than merge sort both in time and space. Are there any mistakes in my answer? Did I miss anything?

Original source