Java等价于 C++ 的 upper_bound() 方法
在本文中,我们将讨论 Java 对 C++ 的 upper_bound() 方法的等效实现。该方法提供了一个在数组中搜索的键值。它返回数组中第一个元素的索引,如果没有找到这样的元素,则它的值大于 key 或 last。下面的实现将找到上限值及其索引,否则将打印上限值不存在。
插图:
Input : 10 20 30 30 40 50
Output : upper_bound for element 30 is 40 at index 4
Input : 10 20 30 40 50
Output : upper_bound for element 45 is 50 at index 4
Input : 10 20 30 40 50
Output : upper_bound for element 60 does not exists
现在让我们讨论一下方法,以便使用 upper bound() 方法来获取下一个更大值的索引。
方法:
- 朴素方法(线性搜索)
- 迭代二分查找
- 递归二分查找
- Arrays 实用程序类的 binarySearch() 方法
让我们通过为它们提供干净的Java程序来讨论上述每种方法以详细了解它们,如下所示:
方法一:使用线性搜索
为了使用线性搜索找到上限,我们将从第 0 个索引开始遍历数组,直到找到大于键的值。
例子
Java
// Java program for finding upper bound
// using linear search
// Importing Arrays utility class
import java.util.Arrays;
// Main class
class GFG {
// Method 1
// To find upper bound of given key
static void upper_bound(int arr[], int key)
{
int upperBound = 0;
while (upperBound < arr.length) {
// If current value is lesser than or equal to
// key
if (arr[upperBound] <= key)
upperBound++;
// This value is just greater than key
else{
System.out.print("The upper bound of " + key + " is " + arr[upperBound] + " at index " + upperBound);
return;
}
}
System.out.print("The upper bound of " + key + " does not exist.");
}
// Method 2
// Main driver method
public static void main(String[] args)
{
// Custom array input over which upper bound is to
// be operated by passing a key
int array[] = { 10, 20, 30, 30, 40, 50 };
int key = 30;
// Sort the array using Arrays.sort() method
Arrays.sort(array);
// Printing the upper bound
upper_bound(array, key);
}
}
Java
// Java program to Find upper bound
// Using Binary Search Iteratively
// Importing Arrays utility class
import java.util.Arrays;
// Main class
public class GFG {
// Iterative approach to find upper bound
// using binary search technique
static void upper_bound(int arr[], int key)
{
int mid, N = arr.length;
// Initialise starting index and
// ending index
int low = 0;
int high = N;
// Till low is less than high
while (low < high && low != N) {
// Find the index of the middle element
mid = low + (high - low) / 2;
// If key is greater than or equal
// to arr[mid], then find in
// right subarray
if (key >= arr[mid]) {
low = mid + 1;
}
// If key is less than arr[mid]
// then find in left subarray
else {
high = mid;
}
}
// If key is greater than last element which is
// array[n-1] then upper bound
// does not exists in the array
if (low == N ) {
System.out.print("The upper bound of " + key + " does not exist.");
return;
}
// Print the upper_bound index
System.out.print("The upper bound of " + key + " is " + arr[low] + " at index " + low);
}
// Driver main method
public static void main(String[] args)
{
int array[] = { 10, 20, 30, 30, 40, 50 };
int key = 30;
// Sort the array using Arrays.sort() method
Arrays.sort(array);
// Printing the upper bound
upper_bound(array, key);
}
}
Java
// Java program to Find Upper Bound
// Using Binary Search Recursively
// Importing Arrays utility class
import java.util.Arrays;
// Main class
public class GFG {
// Recursive approach to find upper bound
// using binary search technique
static int recursive_upper_bound(int arr[], int low,
int high, int key)
{
// Base Case
if (low > high || low == arr.length)
return low;
// Find the value of middle index
int mid = low + (high - low) / 2;
// If key is greater than or equal
// to array[mid], then find in
// right subarray
if (key >= arr[mid]) {
return recursive_upper_bound(arr, mid + 1, high,
key);
}
// If key is less than array[mid],
// then find in left subarray
return recursive_upper_bound(arr, low, mid - 1,
key);
}
// Method to find upper bound
static void upper_bound(int arr[], int key)
{
// Initialize starting index and
// ending index
int low = 0;
int high = arr.length;
// Call recursive upper bound method
int upperBound
= recursive_upper_bound(arr, low, high, key);
if (upperBound == arr.length)
// upper bound of the key does not exists
System.out.print("The upper bound of " + key
+ " does not exist.");
else System.out.print(
"The upper bound of " + key + " is "
+ arr[upperBound] + " at index "
+ upperBound);
}
// Main driver method
public static void main(String[] args)
{
int array[] = { 10, 20, 30, 30, 40, 50 };
int key = 30;
// Sorting the array using Arrays.sort() method
Arrays.sort(array);
// Printing the upper bound
upper_bound(array, key);
}
}
Java
// Java program to find upper bound
// Using binarySearch() method of Arrays class
// Importing Arrays utility class
import java.util.Arrays;
// Main class
public class GFG {
// Method 1
// To find upper bound using binary search
// implementation of Arrays utility class
static void upper_bound(int arr[], int key)
{
int index = Arrays.binarySearch(arr, key);
int n = arr.length;
// If key is not present in the array
if (index < 0) {
// Index specify the position of the key
// when inserted in the sorted array
// so the element currently present at
// this position will be the upper bound
int upperBound = Math.abs(index) - 1;
if (upperBound < n)
System.out.print("The upper bound of " + key
+ " is " + arr[upperBound]
+ " at index "
+ upperBound);
else
System.out.print("The upper bound of " + key
+ " does not exists.");
return;
}
// If key is present in the array
// we move rightwards to find next greater value
else {
// Increment the index until value is equal to
// key
while (index < n) {
// If current value is same
if (arr[index] == key)
index++;
// Current value is different which means
// it is the greater than the key
else {
System.out.print(
"The upper bound of " + key + " is "
+ arr[index] + " at index "
+ index);
return;
}
}
System.out.print("The upper bound of " + key
+ " does not exist.");
}
}
// Method 2
// Main driver method
public static void main(String[] args)
{
int array[] = { 10, 20, 30, 30, 40, 50 };
int key = 30;
// Sort the array before applying binary search
Arrays.sort(array);
// Printing the lower bound
upper_bound(array, key);
}
}
The upper bound of 30 is 40 at index 4
Time Complexity: O(N), where N is the number of elements in the array.
Auxiliary Space: O(1)
要找到键的上限,我们将在数组中搜索键。我们可以使用一种有效的二进制搜索方法在O(log2 n)中搜索排序数组中的键,如下面的示例所示。
方法二:迭代使用二分查找
程序:
- 在应用二进制搜索之前对数组进行排序。
- 初始化低为0,高为N。
- 找到中间元素的索引(mid)
- 将键与中间元素进行比较(arr[mid])
- 如果中间元素小于等于key,则更新low为mid+1,否则更新high为mid。
- 重复第 2 步到第 4 步,直到低值小于高值。
- 在上述所有步骤之后,低位是键的上界
例子
Java
// Java program to Find upper bound
// Using Binary Search Iteratively
// Importing Arrays utility class
import java.util.Arrays;
// Main class
public class GFG {
// Iterative approach to find upper bound
// using binary search technique
static void upper_bound(int arr[], int key)
{
int mid, N = arr.length;
// Initialise starting index and
// ending index
int low = 0;
int high = N;
// Till low is less than high
while (low < high && low != N) {
// Find the index of the middle element
mid = low + (high - low) / 2;
// If key is greater than or equal
// to arr[mid], then find in
// right subarray
if (key >= arr[mid]) {
low = mid + 1;
}
// If key is less than arr[mid]
// then find in left subarray
else {
high = mid;
}
}
// If key is greater than last element which is
// array[n-1] then upper bound
// does not exists in the array
if (low == N ) {
System.out.print("The upper bound of " + key + " does not exist.");
return;
}
// Print the upper_bound index
System.out.print("The upper bound of " + key + " is " + arr[low] + " at index " + low);
}
// Driver main method
public static void main(String[] args)
{
int array[] = { 10, 20, 30, 30, 40, 50 };
int key = 30;
// Sort the array using Arrays.sort() method
Arrays.sort(array);
// Printing the upper bound
upper_bound(array, key);
}
}
The upper bound of 30 is 40 at index 4
下面讨论遵循相同过程的递归方法:
方法三:递归二分查找
例子
Java
// Java program to Find Upper Bound
// Using Binary Search Recursively
// Importing Arrays utility class
import java.util.Arrays;
// Main class
public class GFG {
// Recursive approach to find upper bound
// using binary search technique
static int recursive_upper_bound(int arr[], int low,
int high, int key)
{
// Base Case
if (low > high || low == arr.length)
return low;
// Find the value of middle index
int mid = low + (high - low) / 2;
// If key is greater than or equal
// to array[mid], then find in
// right subarray
if (key >= arr[mid]) {
return recursive_upper_bound(arr, mid + 1, high,
key);
}
// If key is less than array[mid],
// then find in left subarray
return recursive_upper_bound(arr, low, mid - 1,
key);
}
// Method to find upper bound
static void upper_bound(int arr[], int key)
{
// Initialize starting index and
// ending index
int low = 0;
int high = arr.length;
// Call recursive upper bound method
int upperBound
= recursive_upper_bound(arr, low, high, key);
if (upperBound == arr.length)
// upper bound of the key does not exists
System.out.print("The upper bound of " + key
+ " does not exist.");
else System.out.print(
"The upper bound of " + key + " is "
+ arr[upperBound] + " at index "
+ upperBound);
}
// Main driver method
public static void main(String[] args)
{
int array[] = { 10, 20, 30, 30, 40, 50 };
int key = 30;
// Sorting the array using Arrays.sort() method
Arrays.sort(array);
// Printing the upper bound
upper_bound(array, key);
}
}
The upper bound of 30 is 40 at index 4
方法 4:使用 Arrays 实用程序类的 binarySearch() 方法
我们还可以使用 Arrays 实用程序类(或 Collections 实用程序类)的内置二进制搜索方法。此函数返回数组中键的索引。如果键存在于数组中,它将返回其索引(不保证是第一个索引),否则根据排序顺序,它将返回键的预期位置,即(-(插入点)-1)。
方法:
- 在应用二进制搜索之前对数组进行排序。
- 在排序后的数组中搜索键的索引,如果它存在于数组中,则返回其索引为 的正值,否则返回一个负值,指定该键应在排序后的数组中添加的位置。
- 现在,如果数组中存在键,我们向右移动以找到下一个更大的值。
- 打印上限索引(如果存在)。
例子
Java
// Java program to find upper bound
// Using binarySearch() method of Arrays class
// Importing Arrays utility class
import java.util.Arrays;
// Main class
public class GFG {
// Method 1
// To find upper bound using binary search
// implementation of Arrays utility class
static void upper_bound(int arr[], int key)
{
int index = Arrays.binarySearch(arr, key);
int n = arr.length;
// If key is not present in the array
if (index < 0) {
// Index specify the position of the key
// when inserted in the sorted array
// so the element currently present at
// this position will be the upper bound
int upperBound = Math.abs(index) - 1;
if (upperBound < n)
System.out.print("The upper bound of " + key
+ " is " + arr[upperBound]
+ " at index "
+ upperBound);
else
System.out.print("The upper bound of " + key
+ " does not exists.");
return;
}
// If key is present in the array
// we move rightwards to find next greater value
else {
// Increment the index until value is equal to
// key
while (index < n) {
// If current value is same
if (arr[index] == key)
index++;
// Current value is different which means
// it is the greater than the key
else {
System.out.print(
"The upper bound of " + key + " is "
+ arr[index] + " at index "
+ index);
return;
}
}
System.out.print("The upper bound of " + key
+ " does not exist.");
}
}
// Method 2
// Main driver method
public static void main(String[] args)
{
int array[] = { 10, 20, 30, 30, 40, 50 };
int key = 30;
// Sort the array before applying binary search
Arrays.sort(array);
// Printing the lower bound
upper_bound(array, key);
}
}
The upper bound of 30 is 40 at index 4
Note: We can also find the index of the middle element via any one of them
int mid = (high + low)/ 2;
int mid = (low + high) >>> 1;