二分查找法

Posted learnhow

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了二分查找法相关的知识,希望对你有一定的参考价值。

适用场景:当需要在大量有序数据中查找的时候,推荐使用二分查找法(BinarySearch)。

下图演示的是在有序数组[10,11,12,16,18,23,29,33,48,54,57,68,77,84,98]中命中23和未命中50的情况:

技术分享图片

技术分享图片

Java代码示例:

import java.io.*;

public class BinarySearch {
    public static boolean rank(int target, int[] arr) {
        // 数组下标
        int lo = 0;
        int hi = arr.length - 1;
        while (lo <= hi) {
            int mid = lo + (hi - lo) / 2;
            if (target < arr[mid]) {
                hi = mid - 1;
            } else if (target > arr[mid]) {
                lo = mid + 1;
            } else {
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new FileReader("whitelist.txt"));
        int[] whitelist = new int[1000000];
        String temp;
        int i = 0;
        while ((temp = reader.readLine()) != null) {
            whitelist[i++] = Integer.valueOf(temp);
        }

        reader = new BufferedReader(new FileReader("goallist.txt"));
        int[] targetlist = new int[100000];
        i = 0;
        while ((temp = reader.readLine()) != null) {
            targetlist[i++] = Integer.valueOf(temp);
        }
        int sum = 0;
        long start = System.currentTimeMillis();
        for (int j = 0; j < targetlist.length; ++j) {
            if(BinarySearch.rank(targetlist[j], whitelist)) {
                sum++;
            }
        }
        long end = System.currentTimeMillis();
        System.out.println(sum);
        System.out.println(end - start);
    }
}

Qt/C++代码示例:

#include <QtCore/QCoreApplication>
#include <QDebug>
#include <QFile>
#include <QTime>

bool rank(int target, int *arr, int len) {
    int lo = 0;
    int hi = len;

    while (lo <= hi) {
        int mid = lo + (hi - lo) / 2;
        if (target < arr[mid]) {
            hi = mid - 1;
        }
        else if (target > arr[mid]) {
            lo = mid + 1;
        }
        else {
            return true;
        }
    }
    return false;
}
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    // 1000 * 1000 * 4byte = 4MB 必须声明在堆上
    int *whiteArr = new int[1000000];
    int *goalArr = new int[100000];
    QFile whitefile("whitelist.txt");
    int i = 0;

    if (whitefile.open(QIODevice::ReadOnly | QIODevice::Text)) {

        while (!whitefile.atEnd()) {
            QByteArray line = whitefile.readLine();
            QString s(line);
            whiteArr[i++] = s.toInt();
        }
        whitefile.close();
    }

    QFile goalfile("goallist.txt");
    i = 0;
    if (goalfile.open(QIODevice::ReadOnly | QIODevice::Text)) {
        while (!goalfile.atEnd()) {
            QByteArray line = goalfile.readLine();
            QString s(line);
            goalArr[i++] = s.toInt();
        }

        goalfile.close();
    }
    int sum = 0;
    QTime rt;
    rt.start();
    for (int j = 0; j < 100000; ++j) {
        if (rank(goalArr[j], whiteArr, 1000000)) {
            sum++;
        }
    }
    int el = rt.elapsed();
    qDebug() << sum;
    qDebug() << el;
    return a.exec();
}

 相关附件:

whitelist.txt
goallist.txt


以上是关于二分查找法的主要内容,如果未能解决你的问题,请参考以下文章

Jdk之中的二分查找法

简单算法------二分查找法

二分查找法

二分查找法

二分查找法

算法丨二分查找法