Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在未排序的整数数组中查找与给定编号最近匹配的java方法_Java_Arrays_Algorithm_Sorting - Fatal编程技术网

在未排序的整数数组中查找与给定编号最近匹配的java方法

在未排序的整数数组中查找与给定编号最近匹配的java方法,java,arrays,algorithm,sorting,Java,Arrays,Algorithm,Sorting,我想得到有关Java程序的帮助,以便在未排序的整数数组中找到与任何给定整数最接近的匹配项 我可以就以下方面提出建议吗 * How to get start off with this? * Should i first sort the array 谢谢大家是的,然后使用 返回: 搜索键的索引(如果有) 它包含在数组中; 否则,-插入点-1。 插入点被定义为 关键点是什么 插入到数组中:的索引 第一个元素大于 键,如果所有元素都在 数组小于指定的值 钥匙注意,这保证了 如果且 只有找到钥匙 不

我想得到有关Java程序的帮助,以便在未排序的整数数组中找到与任何给定整数最接近的匹配项

我可以就以下方面提出建议吗

* How to get start off with this?
* Should i first sort the array
谢谢大家

是的,然后使用

返回: 搜索键的索引(如果有) 它包含在数组中; 否则,-插入点-1。 插入点被定义为 关键点是什么 插入到数组中:的索引 第一个元素大于 键,如果所有元素都在 数组小于指定的值 钥匙注意,这保证了 如果且 只有找到钥匙


不,不需要对数组进行预排序。只需运行它,记录当前最接近匹配的位置和值,必要时在每次迭代中更新它。这需要时间,而排序将需要lg n,除非您进行计数排序,这并不总是适用的

只有重复执行此操作,排序才会有效果。

如果只需要执行一次搜索,则可以从头到尾扫描数组,跟踪最接近正在搜索的值


如果需要在同一数组中重复搜索,应先对数组进行预排序,然后重复使用二进制搜索。

不要先对数组排序,因为它会修改原始数组


相反,在数组中循环,跟踪当前数组元素与给定值之间的差异以及到目前为止差异最小的数组元素之间的差异。这里的复杂性是线性的;排序比不上排序。

如果无法对数组排序,或者只执行一次,则可以执行此操作

public static int closest1(int find, int... values) {
    int closest = values[0];
    for(int i: values)
       if(Math.abs(closest - find) > Math.abs(i - find))
           closest = i;
    return closest;
}
这将返回一个最接近的值。如果要查找两个值之间相等的值,则将得到第一个值

经过优化的版本

public static int closest2(int find, int... values) {
    int closest = values[0];
    int distance = Math.abs(closest - find);
    for(int i: values) {
       int distanceI = Math.abs(i - find);
       if(distance > distanceI) {
           closest = i;
           distance = distanceI;
       }
    }
    return closest;
}
多线程版本

public static int closest3(final int find, final int... values) {
    final int procs = Runtime.getRuntime().availableProcessors();
    ExecutorService es = Executors.newFixedThreadPool(procs);
    List<Future<Integer>> futures = new ArrayList<Future<Integer>>();
    final int blockSize = values.length / procs;
    for (int i = 0; i < procs; i++) {
        final int start = blockSize * i;
        final int end = Math.min(blockSize * (i + 1), values.length);
        futures.add(es.submit(new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                int closest = values[start];
                int distance = Math.abs(closest - find);
                for (int i = start + 1; i < end; i++) {
                    int n = values[i];
                    int distanceI = Math.abs(n - find);
                    if (distance > distanceI) {
                        closest = i;
                        distance = distanceI;
                    }
                }
                return closest;
            }
        }));
    }
    es.shutdown();
    int[] values2 = new int[futures.size()];
    try {
        for (int i = 0; i < futures.size(); i++)
            values2[i] = futures.get(i).get();
        return closest2(find, values2);
    } catch (Exception e) {
        throw new AssertionError(e);
    }
}

使用第二种方法,每百万个条目可节省0.8毫秒。第三种方法对于大型数组要快得多,但对于较小的数组则要慢得多。

我不知道如何使用binarySearch(一种搜索精确匹配的方法)来查找与提供的整数最接近的整数。binarySearch只会返回精确匹配的索引。@Anthony,@Damokles也许你应该阅读文档,然后,我添加了相关的摘录BinarySearch,如果没有找到值,它将返回插入点的否定。虽然这不是最快的解决方案,也不是最简单的。@Sean这现在更有意义了。如果将closestin的偏移量存储在一个变量中而不是进行数学运算,它不会显著降低复杂性吗。absclosest-反复查找?可能还可以避免使用abs。哦,如果数组为空,此方法将抛出ArrayIndexOutOfBoundsException:-在这种情况下,没有最近的条目;
/**
 * @return the index of the closest match to the given value
 */
int nearestMatch(int[] array, int value) {
    if (array.length == 0) {
        throw new IllegalArgumentException();
    }
    int nearestMatchIndex = 0;
    for (int i = 1; i < array.length; i++) {
        if ( Math.abs(value - array[nearestMatchIndex])
                > Math.abs(value - array[i]) ) {
            nearestMatchIndex = i;
        }
    }
    return nearestMatchIndex;
}
closest1 took 623 ms, closest2 took 499 ms, closest3 took 181 ms 
closest1 took 645 ms, closest2 took 497 ms, closest3 took 145 ms 
closest1 took 625 ms, closest2 took 495 ms, closest3 took 134 ms 
closest1 took 626 ms, closest2 took 494 ms, closest3 took 134 ms 
closest1 took 627 ms, closest2 took 495 ms, closest3 took 134 ms 
/**
 * @return the index of the closest match to the given value
 */
int nearestMatch(int[] array, int value) {
    if (array.length == 0) {
        throw new IllegalArgumentException();
    }
    int nearestMatchIndex = 0;
    for (int i = 1; i < array.length; i++) {
        if ( Math.abs(value - array[nearestMatchIndex])
                > Math.abs(value - array[i]) ) {
            nearestMatchIndex = i;
        }
    }
    return nearestMatchIndex;
}