小编use*_*796的帖子

查找数字数组中两个最接近元素之间的距离

所以我从我购买的这本书中自学了算法,并且我有一个伪代码,用于查找数字数组中两个最简洁元素之间的距离

MinDistance(a[0...n-1])
Input: Array A of numbers
Output: Minimum Distance between two of its elements
dMin <- maximum integer

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
Run Code Online (Sandbox Code Playgroud)

但是,我想对这个算法解决方案进行改进.改变已经存在的东西,或者一起重写.有人可以帮忙吗?我用Java编写函数和类来测试伪代码?那是对的吗?再一次,我怎样才能从效率的角度来改善它.

//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){ …
Run Code Online (Sandbox Code Playgroud)

java algorithm

4
推荐指数
1
解决办法
2万
查看次数

标签 统计

algorithm ×1

java ×1