📜  使用二进制搜索搜索 ArrayList 元素的Java程序

📅  最后修改于: 2022-05-13 01:55:11.818000             🧑  作者: Mango

使用二进制搜索搜索 ArrayList 元素的Java程序

线性搜索可以实现对特定数据结构的数据结构的排序和非排序元素,但平均情况时间复杂度为 O(n)。而二分搜索只有在项目按排序顺序并且平均时间复杂度为 O(logn) 并且横向都具有最佳时间复杂度为 O(1) 时才能实现。现在,给定一个包含已排序元素的 Array List 检查该元素是否存在于 ArrayList 中。

在线性数据结构中搜索元素时有两种类型的横向。

  1. 线性搜索
  2. 二分搜索。

插图:

Input:

ArrayList:[1, 2, 3, 4, 6, 7, 8, 9]
key:3

Output:
true

案例 1:使用二分搜索 因为列表是按顺序排序的,二分搜索的平均时间复杂度低于线性搜索,即 O(logn)。

Java
// Java Program to Search ArrayList Element
// Using Binary Search
  
// Importing generic java libraries
import java.io.*;
import java.util.*;
  
class GFG {
  
    // Method to search elements in ArrayList
    static boolean search(int key, ArrayList A)
    {
        // low pointer
        int low = 0;
  
        // high pointer
        int high = A.size() - 1;
  
        while (low <= high) {
  
            // find the mid pointer
            int mid = low + (high - low) / 2;
            if (A.get(mid) == key) {
                return true;
            }
            else if (A.get(mid) < key) {
  
                // shift the low pointer
                low = mid + 1;
            }
            else {
  
                // shift the high pointer
                high = mid - 1;
            }
        }
        return false;
    }
  
    // Main driver method
    public static void main(String[] args)
    {
  
        // Creating an ArrayList
        ArrayList A = new ArrayList<>();
  
        // Adding items in the list
        A.add(1);
        A.add(2);
        A.add(3);
        A.add(4);
        A.add(6);
        A.add(7);
        A.add(8);
        A.add(9);
  
        // Random element to be searched
        int key = 19;
  
        // Binary search
        boolean check = search(key, A);
  
        System.out.println(check);
  
        int key1 = 2;
  
        // Binary search
        boolean check1 = search(key1, A);
  
        System.out.println(check1);
    }
}


Java
// Java Program to Search ArrayList Element
// Using Binary Search
  
// Importing generic java libraries
import java.io.*;
import java.util.*;
  
class GFG {
  
    // Method to search elements in arrayList
    static int search(int key, ArrayList A)
    {
        // Low pointer
        int low = 0;
  
        // High pointer
        int high = A.size() - 1;
        int index = -1;
        while (low <= high) {
  
            // Mid pointer
            int mid = low + (high - low) / 2;
            if (A.get(mid) == key) {
  
                // Shift the High Pointer
                high = mid - 1;
            }
            else if (A.get(mid) < key) {
  
                // Storing the index of mid index value
                index = mid;
  
                // Shift the Low Pointer
                low = mid + 1;
            }
  
            else {
                high = mid - 1;
            }
        }
  
        // Return the index
        return index;
    }
  
    public static void main(String[] args)
    {
        // Creating an ArrayList
        ArrayList A = new ArrayList<>();
  
        // Adding elements in ArrayList
        A.add(2);
        A.add(3);
        A.add(3);
        A.add(4);
        A.add(4);
        A.add(5);
        A.add(6);
        A.add(7);
  
        // Key
        int key = 5;
  
        // Index of smallest greater element
        int index = search(key, A);
  
        // Print searched element
        System.out.println(index);
    }
}


输出:

false
true

案例2:假设为了在ArrayList的已排序重复元素中使用二分搜索找到小于键的最大元素的最大索引。

Input:

List: [2, 3, 3, 4, 4, 5, 6, 7]
key: 2
key: 4

Output:
-1
2

示例:根据条件修改二分查找

Java

// Java Program to Search ArrayList Element
// Using Binary Search
  
// Importing generic java libraries
import java.io.*;
import java.util.*;
  
class GFG {
  
    // Method to search elements in arrayList
    static int search(int key, ArrayList A)
    {
        // Low pointer
        int low = 0;
  
        // High pointer
        int high = A.size() - 1;
        int index = -1;
        while (low <= high) {
  
            // Mid pointer
            int mid = low + (high - low) / 2;
            if (A.get(mid) == key) {
  
                // Shift the High Pointer
                high = mid - 1;
            }
            else if (A.get(mid) < key) {
  
                // Storing the index of mid index value
                index = mid;
  
                // Shift the Low Pointer
                low = mid + 1;
            }
  
            else {
                high = mid - 1;
            }
        }
  
        // Return the index
        return index;
    }
  
    public static void main(String[] args)
    {
        // Creating an ArrayList
        ArrayList A = new ArrayList<>();
  
        // Adding elements in ArrayList
        A.add(2);
        A.add(3);
        A.add(3);
        A.add(4);
        A.add(4);
        A.add(5);
        A.add(6);
        A.add(7);
  
        // Key
        int key = 5;
  
        // Index of smallest greater element
        int index = search(key, A);
  
        // Print searched element
        System.out.println(index);
    }
}

输出:

4