Monday, May 7, 2012

Challenge 066’s Solution

Of the five options, the most efficient would be option B: to use a Selection Sort that terminates after 5 iterations. Option A offers to use a complete Selection Sort. A Selection Sort starts by getting an array list of size n. It searches each element in this list, and places the largest at position n, then repeats for an array list of size n − 1 until the array has a size of 1. If performed, all 2,000 cows would be ordered. Since we only need the 5 heaviest cows, option A isn’t the most efficient. Option B offers to use Selection Sort, but only for five executions. If performed, the top 5 heaviest cows would be ordered, while the remaining cows would not. Since there is no unnecessary sorting, option B is the most efficient. Option C offers to use a complete Insertion Sort. An Insertion Sort starts by getting an array list and an int variable n of size 1. The value of the element n + 1 is saved separately. Then, among the first n elements of the array, the saved value is inserted in numeric order, and values greater than it are pushed one element right. This repeats with n + 1 until n is the size of the array. If performed, all 2,000 cows would be ordered. Since we only need the 5 heaviest cows, option C isn’t the most efficient. Option D offers to use Insertion Sort, but only for five executions. If performed, only the first 5 cows who were weighed would be ordered, while the remaining cows would not. Since the first 5 cows ordered may not necessarily be the 5 heaviest, option D is inefficient. Finally, option E offers to use a complete Mergesort. A Mergesort starts by splitting the array into two equal halves. Each half is sorted separately, then combined into one sorted whole. If performed, all 2,000 cows would be ordered. Since we only need the 5 heaviest cows, option E isn’t the most efficient.
 
−Phoenix

No comments:

Post a Comment