This is a question from an interview I recently found on the Internet:
If you are going to implement a function that takes an integer array as input and returns the maximum, do you use bubble sort or merge sort to implement this function? What if the array size is less than 1000? What if it is more than 1000?
Here's how I think about it:
Firstly, it is very strange to use sorting to implement the above function. You can just go through the array once and find the maximum. Secondly, if you need to make a choice between them, it is better to choose a bubble - you do not need to perform the entire procedure for sorting the bubbles, but only need to complete the first pass. This is better than merging sorting in both time and space.
Are there any errors in my answer? Did I miss something?
source
share