Explanation of how to make an algorithm more efficient


Question:

Explanation of how to make an algorithm more efficient

Algorithm called MinDistance that determines the distance between the two closest elements in an array. I need to make it more efficient.

Here is the algorithm, written in pseudocode:

MinDistance(A[0...n-1]
dmin <-- infinity
for i <--- 0 to n - 1 do
for j <--- 0 to n - 1 do
if i does not equal j and |A|i| - A|j|| < dmin
dmin <--- |A|i| - A|j||
return dmin

To me, this feels like it's in its most efficient form, but I'm wrong. Can you help?

Solution Preview :

Prepared by a verified Expert
Data Structure & Algorithms: Explanation of how to make an algorithm more efficient
Reference No:- TGS01936845

Now Priced at $20 (50% Discount)

Recommended (93%)

Rated (4.5/5)