当前位置:网站首页>Binary search the specified number of numbers in the array binary advanced
Binary search the specified number of numbers in the array binary advanced
2022-06-12 23:55:00 【< I can't do anything;】
Use binary search to count the number of the same number as the target number in the array
The idea of the algorithm is based on the binary search number , The purpose is to count how many numbers in the array are equal to the target number .
notes :
- When used directly for Loop through the array , Each iteration is compared once , The time complexity will be O(n)
- If you use a binary search , First find a subscript equal to the target number , Use while Centered around this subscript , Compare left or right . If the same number is only 2 If you want to , Then search left and right once and you will get the result , But if all the numbers in an array are the same , Then we still need to compare n Time , The time complexity of searching according to this idea is also O(n).
- The idea I use here is First use a binary search to find the left boundary of this number , Then, when the left boundary is found, the right boundary of the number is found by using a bisection search , The difference between the right bound and the left bound is the number of this number in the array . The time complexity of this algorithm is the same as that of the general bisection algorithm . All are O(lgN).
import java.util.Scanner;
public class BinarySearch_findSameNum {
public static void main(String[] args) {
int arr[] = {
1, 1, 5, 5, 5, 8, 8, 9, 10, 15, 15}; // Define an ordered binary array
int target = new Scanner(System.in).nextInt(); // Enter the number of lookups
left(arr, 0, arr.length, target); // The first two points Find the left boundary
}
// Find the function of the left boundary left
private static void left(int[] arr, int low, int high, int target) {
while (low <= high) {
// Definition while The end condition of the loop When subscript low>high Jump out when while loop
int middle = (low + high) / 2;// Be careful there middle The way of calculation will have a great impact I still use the method of adding to get the median
if (arr[middle] >= target) {
// Here we need to find the left boundary , So when arr[middle] Greater than or equal to target when , Then it is proved that the left boundary is low~middle-1 Between
// Be careful When looking for a number in two , When arr[middle]==target You will find the result you want to find , But this is not the end .
// Because the target number in the array has 2 Or 2 More than one time , The index found is probably not the leftmost one , You also need to cycle , Until you find the left boundary
high = middle - 1; // Change superscript , Continue traversing
} else {
low = middle + 1;
}
}// When constantly while When traversing a loop ,low The value of must be greater than high
// The subscript that determines the end of the last loop low Whether the corresponding array value is the same as target equal
if (arr[low] == target && low < arr.length) {
System.out.println("left search success:" + low);
// equal , Then the left boundary exists . First obtain the left boundary of the target number low
int right = right(arr, low+1, arr.length - 1, target); // Looking for a bounded function in a call right
// Be careful The subscript and superscript passed to the right boundary function here are different from those when finding the left boundary Because we have found the left boundary , All the data on the right side of the left boundary of the array can be directly passed to the function
// The subscript can take low+1
// The superscript here is arr.length - 1 The length of the array minus 1 !!!! This -1 It's very important , Corresponding middle Calculation method of .
// Because I add and divide superscript and subscript directly 2 Of Take this topic for example . Find the function to find the right boundary . To find 15 When the left boundary is found 9 when Then superscript 11 Transfer the past middle Calculated 10
// arr[10]=15 Exactly equal to target, At this point, the subscript low Add 1.low=11 After that, the array will be out of bounds So will arr.length-1 Transfer the past Superscript 10, There will be no subscript +1 Post crossing
System.out.println(" Array with " + target + " The same is :" + (right - low + 1) + " individual ");
} else {
System.out.println("error");
}
}
// Find the function of the right boundary right
private static int right(int[] arr, int low, int high, int target) {
// It's easy to find the right boundary , The idea is the same as finding the left boundary
while (low <= high) {
int middle = (high + low) / 2;
if (arr[middle] <= target) {
// If arr[middle] <= target Just keep changing the value of the subscript , until low>high Jump out of while loop
low = middle + 1;
} else {
high = middle - 1;
}
}
if (arr[high] == target && high < arr.length) {
System.out.println("right search find:" + high);
return high; // Returns the index of the right boundary
}
}
}
I wrote it in my spare time , If there are mistakes, please correct them
边栏推荐
- Online examination questions for September examination of financial management
- Why study PMP?
- 华为云弹性云服务器ECS使用【华为云至简致远】
- array
- Start of u-boot_ Armboot analysis (I)
- Start blogging
- PMP renewal | PDU specific operation diagram
- PLC也能制作小遊戲----Codesys編寫猜數字小遊戲
- 2022年R2移动式压力容器充装考试题及在线模拟考试
- leaflet中如何通过透明度控制layerGroup的显示隐藏
猜你喜欢

Tsinghua-Bosch Joint ML Center, THBI Lab:Chengyang Ying | 通过约束条件风险价值实现安全强化学习

OSM map local publishing - how to generate vector maps of provinces and cities

PLC也能制作小游戏----Codesys编写猜数字小游戏

Leetcode 2200. 找出数组中的所有 K 近邻下标(可以,一次过)

How to gracefully solve the offset problem of Baidu and Gaode maps in leaflet

2022 electrician (elementary) operation certificate examination question bank and online simulation examination

PLC也能制作小遊戲----Codesys編寫猜數字小遊戲

dict和set的基本操作

Day 3 of jsbom and DOM learning

Accelerating with Dali modules
随机推荐
VHDL编程实验练习题合集
H5時代leaflet中還在用DivIcon?
【Matlab】基础运算
2022年G3锅炉水处理考题模拟考试平台操作
2022 electrician (elementary) operation certificate examination question bank and online simulation examination
[matlab] basic operation
进程间通信-共享内存shmat
Accelerating with Dali modules
如何实现OSM地图本地发布并自定义配图
數組
Free lottery --- PMP renewal PDU | PMP knowledge map
3、 Storage system
PLC也能制作小游戏----Codesys编写猜数字小游戏
What can PMP bring to you
vs studio_ How to use scanf in 2022
设计消息队列存储信息数据的MySQL表结构
Basic operations of dict and set
Learn to divide subnets in an article
The most complete preview! Huawei cloud wonderful agenda collection
PMP test experience