Here is the algorithm, written in pseudocode:
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||
To me, this feels like it's in its most efficient form, but I'm wrong. Can you help?
Thank you very much.© BrainMass Inc. brainmass.com March 21, 2019, 4:57 pm ad1c9bdddf
There are a couple things that can make this algorithm more efficient. First, calculating the distance between the two elements can be done in one place and stored in a variable. Then that variable can be used for ...
This solution takes an n-squared algorithm for finding the two closest elements in an array and shows how that algorithm can be converted to a much faster algorithm.