在 ArrayList 上执行二进制搜索的Java程序
这 ArrayList类是集合框架的一部分,存在于Java.util 包中。它在Java中为我们提供了可调整大小或动态的数组。它比标准数组慢得多,但在某些需要编写更简洁、更短的代码以及需要对数组进行大量操作的程序中很有帮助。
在已排序数组中搜索元素的最有效算法是二分搜索算法。在本文中,我们将使用Java ArrayList 来实现它。
方法:
在Java ArrayList 上实现二分查找的方法有3 种,下面简要介绍概念,然后用Java例子说明实现部分。
- 迭代二分搜索(使用循环的普通二分搜索)
- 递归二分搜索(使用递归的二分搜索)
- 使用Java集合内置的binarySearch方法。
方法一:迭代二分查找
在这种方法中,我们在一次比较后忽略了一半的元素。由于数组已排序。
- 将要搜索的给定值与中间元素进行比较。
- 如果它与中间元素匹配,我们返回 x。
- 如果它大于中间元素,则对右子数组执行相同操作。即。数组的大小减少到一半,我们要比较的数组是右子数组。
- 如果它小于中间元素,则对左子数组执行相同的操作。即。数组的大小减少到一半,我们要比较的数组是左子数组。
- 如果我们不返回任何内容但搜索结束,则返回 -1,这意味着该元素不存在于该数组中。
Java
// Java program to print binary search over an ArrayList
import java.io.*;
import java.util.*;
class BinarySearch
{
// Returns index of x if it is present in arr[],
// else return -1
int binarySearch(ArrayList arr, int x)
{
int left = 0, right = arr.size() - 1;
while (left <= right)
{
int mid = left + (right - left) / 2;
// Check if x is present at mid
if (arr.get(mid) == x)
return mid;
// If x greater, ignore left half
if (arr.get(mid) < x)
left = mid + 1;
// If x is smaller, ignore right half
else
right = mid - 1;
}
// if we reach here, then element was
// not present
return -1;
}
// Driver method to test above
public static void main(String args[])
{
BinarySearch ob = new BinarySearch();
ArrayList arr = new ArrayList();
arr.add(5);
arr.add(10);
arr.add(15);
arr.add(20);
arr.add(25);
arr.add(30);
arr.add(35);
int x = 10;
// Printing elements of array list
System.out.println("The elements of the arraylist are: "+arr);
int result = ob.binarySearch(arr, x);
if (result == -1)
System.out.println("Element not present");
else
System.out.println("The Element " + x + " is found at "
+ "index " + result);
}
}
Java
// Java implementation of recursive Binary Search
import java.io.*;
import java.util.*;
class BinarySearch
{
// Returns index of x if it is present in arr[l..
// r], else return -1
int binarySearch(ArrayList arr, int l, int r, int x)
{
if (r >= l)
{
int mid = l + (r - l) / 2;
// If the element is present at the
// middle itself
if (arr.get(mid) == x)
return mid;
// If element is smaller than mid, then
// it can only be present in left subarray
if (arr.get(mid) > x)
return binarySearch(arr, l, mid - 1, x);
// Else the element can only be present
// in right subarray
return binarySearch(arr, mid + 1, r, x);
}
// We reach here when element is not present
// in array
return -1;
}
// Driver method to test above
public static void main(String args[])
{
BinarySearch ob = new BinarySearch();
ArrayList arr = new ArrayList();
arr.add(5);
arr.add(10);
arr.add(15);
arr.add(20);
arr.add(25);
arr.add(30);
arr.add(35);
int n = arr.size();
// We will find x inside the arraylist
int x = 10;
// Printing elements of array list
System.out.println("The elements of the arraylist are: "+arr);
int result = ob.binarySearch(arr,0,n-1,x);
if (result == -1)
System.out.println("Element not present");
else
System.out.println("The Element " + x + " is found at "
+ "index " + result);
}
}
Java
// Java program to demonstrate the searching of
// an element in ArrayList using binarySearch()
// of Collections class
import java.util.ArrayList;
import java.util.Collections;
public class BinarySearch {
public static void main(String[] args)
{
ArrayList arr = new ArrayList();
arr.add(5);
arr.add(10);
arr.add(15);
arr.add(20);
arr.add(25);
arr.add(30);
arr.add(35);
// Initializing the key to be found.
int val = 10;
// Printing elements of array list
System.out.println("The elements of the arraylist are: "+arr);
// Implementing the built-in binarySearch method from collections
int result = Collections.binarySearch(arr,val);
if (result == -1)
System.out.println("Element not present");
else
System.out.println("The Element " + val + " is found at "
+ "index " + result);
}
}
输出
The elements of the arraylist are: [5, 10, 15, 20, 25, 30, 35]
The Element 10 is found at index 1
方法二:递归二分查找
- 将要搜索的元素 t (x) 与中间元素进行比较。
- 如果 x 与中间元素匹配,我们返回中间索引。
- Else 如果 x 大于中间元素,则 x 只能位于中间元素之后的右半子阵列中。所以我们在右半场重复。
- 否则(x 较小)在左半部分重复出现。
Java
// Java implementation of recursive Binary Search
import java.io.*;
import java.util.*;
class BinarySearch
{
// Returns index of x if it is present in arr[l..
// r], else return -1
int binarySearch(ArrayList arr, int l, int r, int x)
{
if (r >= l)
{
int mid = l + (r - l) / 2;
// If the element is present at the
// middle itself
if (arr.get(mid) == x)
return mid;
// If element is smaller than mid, then
// it can only be present in left subarray
if (arr.get(mid) > x)
return binarySearch(arr, l, mid - 1, x);
// Else the element can only be present
// in right subarray
return binarySearch(arr, mid + 1, r, x);
}
// We reach here when element is not present
// in array
return -1;
}
// Driver method to test above
public static void main(String args[])
{
BinarySearch ob = new BinarySearch();
ArrayList arr = new ArrayList();
arr.add(5);
arr.add(10);
arr.add(15);
arr.add(20);
arr.add(25);
arr.add(30);
arr.add(35);
int n = arr.size();
// We will find x inside the arraylist
int x = 10;
// Printing elements of array list
System.out.println("The elements of the arraylist are: "+arr);
int result = ob.binarySearch(arr,0,n-1,x);
if (result == -1)
System.out.println("Element not present");
else
System.out.println("The Element " + x + " is found at "
+ "index " + result);
}
}
输出
The elements of the arraylist are: [5, 10, 15, 20, 25, 30, 35]
The Element 10 is found at index 1
方法三:使用Collections类内置的binarySearch方法
在这个方法中,我们只调用集合框架的 binarySearch() 方法并将我们排序的 ArrayList 和要搜索的值解析到该方法中,这将返回元素的索引(如果存在),否则返回 -1。
Java
// Java program to demonstrate the searching of
// an element in ArrayList using binarySearch()
// of Collections class
import java.util.ArrayList;
import java.util.Collections;
public class BinarySearch {
public static void main(String[] args)
{
ArrayList arr = new ArrayList();
arr.add(5);
arr.add(10);
arr.add(15);
arr.add(20);
arr.add(25);
arr.add(30);
arr.add(35);
// Initializing the key to be found.
int val = 10;
// Printing elements of array list
System.out.println("The elements of the arraylist are: "+arr);
// Implementing the built-in binarySearch method from collections
int result = Collections.binarySearch(arr,val);
if (result == -1)
System.out.println("Element not present");
else
System.out.println("The Element " + val + " is found at "
+ "index " + result);
}
}
输出
The elements of the arraylist are: [5, 10, 15, 20, 25, 30, 35]
The Element 10 is found at index 1