Question: Consider the following algorithm for finding the distance between the two closest elements in an array of numbers. Algorithm MinDistance(A[0..n 1]) //Input: Array A[0..n 1]
Consider the following algorithm for finding the distance between the two closest elements in an array of numbers.
Algorithm MinDistance(A[0..n 1]) //Input: Array A[0..n 1] of numbers //Output: Minimum distance between two of its elements dmin for i 0 to n 1 do
for j 0 to n 1 do if |A[i] A[j]| < dmin
dmin |A[i] A[j]|
return dmin
(a) What is the basic operation of this algorithm? How many times is it performed as a function of the array size n?
(b) Make as many improvements as you can in this algorithmic solution to the problem. You must write down the pseudocode for your new algorithm. How many times is the basic operation performed as a function of the array size n? (If you need to, you may change the algorithm altogether; if not, improve the implementation given.)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
