Consider the following algorithm for finding the distance between the two closes
ID: 3628300 • Letter: C
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]) 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 i j and |A[i] - A[j]| < dmin
dmin |A[i] - A[j]|
return dmin
Make as many improvements as you can in this algorithmic solution to the problem. (If you need to, you may change the algorithm altogether, if not, improve the implementation given.)
Explanation / Answer
//Scanner library allowing the user to input data import java.lang.Math.*; public class ArrayTester{ //algorithm for finding the distance between the two closest elements in an array of numbers public int MinDistance(int [] ar){ int [] a = ar; int aSize = a.length; int dMin = 0;//MaxInt for(int i=0; iRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.