Algs4-1.4.17最遥远的一对(一维)

1.4.17最遥远的一对(一维)。编写一个程序,给定一个含有N个double值的数组a[],在其中找到一对最遥远的值:两者之差(绝对值)最大的两个数。程序在最坏情况下所需的运行时间应该是线性级别的。
答:
import java.util.Arrays;
public class TheFast
{
    public static void fast(double[] a)
    {
        int minIndex=0;
        int maxIndex=0;
        for(int i=0;i<a.length;i++)
        {
            if(a[i]<a[minIndex])
              minIndex=i;
            else if (a[i]>a[maxIndex])
              maxIndex=i;
        }//end for
      StdOut.printf("minIndex=%d,maxIndex=%d",minIndex,maxIndex);
    }
   
    public static void main(String[] args)
    {
         double[] a=In.readDoubles(args[0]);
         fast(a);
     }
}

原文地址:https://www.cnblogs.com/longjin2018/p/9854434.html