数据结构与算法——二分查找
二分查找对有序数组进行查找,循环将待查找的值与数组中点值比较,并缩小值所在范围,直到找到值
1、常规二分查找
public class BinarySearch {
public static void main(String[] args) {
final SecureRandom random = new SecureRandom();
final int[] arr = new int[100];
for (int i=0;i<100;i++){
arr[i] = random.nextInt(20000);
}
int searchValue = arr[random.nextInt(99)];
FastSort.sort(arr);
System.out.println("排序后的数组:"+Arrays.toString(arr));
System.out.println("要找的值:"+searchValue);
int searchIndex = search(arr, searchValue);
System.out.println("找到的位置:"+ searchIndex);
System.out.println("位置上的值:" + arr[searchIndex]);
}
public static int search(int[] arr,int value){
int minIndex=0;
int maxIndex=arr.length-1;
while (minIndex <= maxIndex){
//加差的一半,是防止大数相加溢出;位运算是为了快
int midIndex = minIndex + ((maxIndex - minIndex) >> 1);
if(value < arr[midIndex]){
//要找的值比中间值小,就把范围缩小到前一半
maxIndex = midIndex-1;
}else if(value > arr[midIndex]){
//要找的值比中间值大,就把范围缩小到后一半
minIndex = midIndex+1;
}else {
return midIndex;
}
}
return -1;
}
}
2、查找第一个值等于给定值的元素
public class BinarySearch {
public static void main(String[] args) {
final SecureRandom random = new SecureRandom();
final int[] arr = new int[500];
for (int i = 0; i < 500; i++) {
arr[i] = random.nextInt(100);
}
int searchValue = arr[random.nextInt(499)];
FastSort.sort(arr);
System.out.println("排序后的数组:" + Arrays.toString(arr));
System.out.println("要找的值:" + searchValue);
int searchIndex = searchFirstEq(arr, searchValue);
System.out.println("找到的位置:" + searchIndex);
System.out.println("位置上的值:" + arr[searchIndex]);
if (searchIndex != 0) {
System.out.println("前一个位置上的值:" + arr[searchIndex - 1]);
}
}
public static int searchFirstEq(int[] arr, int value) {
int minIndex = 0;
int maxIndex = arr.length - 1;
while (minIndex <= maxIndex) {
//加差的一半,是防止大数相加溢出;位运算是为了快
int midIndex = minIndex + ((maxIndex - minIndex) >> 1);
if (value < arr[midIndex]) {
//要找的值比中间值小,就把范围缩小到前一半
maxIndex = midIndex - 1;
} else if (value > arr[midIndex]) {
//要找的值比中间值大,就把范围缩小到后一半
minIndex = midIndex + 1;
} else {
//已经找到要找的值了
if (midIndex == 0 || arr[midIndex - 1] < value) {
//是数组第一个元素,他前一个元素比他小,说明这是第一个值等于给定值的元素
return midIndex;
} else {
//说明前面还有相同元素,继续向前二分查找
maxIndex = midIndex - 1;
}
}
}
return -1;
}
}
3、查找最后一个值等于给定值的元素
public class BinarySearch {
public static void main(String[] args) {
final SecureRandom random = new SecureRandom();
final int[] arr = new int[500];
for (int i = 0; i < 500; i++) {
arr[i] = random.nextInt(100);
}
int searchValue = arr[random.nextInt(499)];
FastSort.sort(arr);
System.out.println("排序后的数组:" + Arrays.toString(arr));
System.out.println("要找的值:" + searchValue);
int searchIndex = searchLastEq(arr, searchValue);
System.out.println("找到的位置:" + searchIndex);
System.out.println("位置上的值:" + arr[searchIndex]);
if(searchIndex != arr.length-1){
System.out.println("后一个位置上的值:" + arr[searchIndex + 1]);
}
}
public static int searchLastEq(int[] arr, int value) {
int minIndex = 0;
int maxIndex = arr.length - 1;
while (minIndex <= maxIndex) {
//加差的一半,是防止大数相加溢出;位运算是为了快
int midIndex = minIndex + ((maxIndex - minIndex) >> 1);
if (value < arr[midIndex]) {
//要找的值比中间值小,就把范围缩小到前一半
maxIndex = midIndex - 1;
} else if (value > arr[midIndex]) {
//要找的值比中间值大,就把范围缩小到后一半
minIndex = midIndex + 1;
} else {
if (midIndex == arr.length - 1 || arr[midIndex + 1] > value) {
return midIndex;
} else {
minIndex = midIndex - 1;
}
}
}
return -1;
}
}
4、查找第一个大于等于给定值的元素
public class BinarySearch {
public static void main(String[] args) {
final SecureRandom random = new SecureRandom();
final int[] arr = new int[500];
for (int i = 0; i < 500; i++) {
arr[i] = random.nextInt(100);
}
int searchValue = arr[random.nextInt(500)];
FastSort.sort(arr);
System.out.println("排序后的数组:" + Arrays.toString(arr));
System.out.println("要找的值:" + searchValue);
int searchIndex = searchFirstMoreOrEq(arr, searchValue);
System.out.println("找到的位置:" + searchIndex);
System.out.println("位置上的值:" + arr[searchIndex]);
if (searchIndex != arr.length - 1) {
System.out.println("前一个位置上的值:" + arr[searchIndex - 1]);
}
}
public static int searchFirstMoreOrEq(int[] arr, int value) {
int minIndex = 0;
int maxIndex = arr.length - 1;
while (minIndex <= maxIndex) {
//加差的一半,是防止大数相加溢出;位运算是为了快
int midIndex = minIndex + ((maxIndex - minIndex) >> 1);
if (arr[midIndex] >= value) {
if (midIndex == 0 || arr[midIndex - 1] < value) {
return midIndex;
} else {
//中间值比给定值大或相等,就把范围缩小到前一半
maxIndex = midIndex - 1;
}
} else {
//中间值比给定值小,说明期望值在中间值右侧,将范围缩小到后面一半
minIndex = midIndex + 1;
}
}
return -1;
}
}
5、查找最后一个小于等于给定值的元素
package com.lcy.data_structure_and_algorithm;
import java.security.SecureRandom;
import java.util.Arrays;
/**
* 二分查找/折半查找
*/
public class BinarySearch {
public static void main(String[] args) {
final SecureRandom random = new SecureRandom();
int[] arr = new int[500];
for (int i = 0; i < 500; i++) {
arr[i] = random.nextInt(100);
}
int searchValue = arr[random.nextInt(500)];
FastSort.sort(arr);
System.out.println("排序后的数组:" + Arrays.toString(arr));
System.out.println("要找的值:" + searchValue);
int searchIndex = searchLastLessOrEq(arr, searchValue);
System.out.println("找到的位置:" + searchIndex);
System.out.println("位置上的值:" + arr[searchIndex]);
if (searchIndex != arr.length - 1) {
System.out.println("后一个位置上的值:" + arr[searchIndex + 1]);
}
}
public static int searchLastLessOrEq(int[] arr, int value) {
int minIndex = 0;
int maxIndex = arr.length - 1;
while (minIndex <= maxIndex) {
//加差的一半,是防止大数相加溢出;位运算是为了快
int midIndex = minIndex + ((maxIndex - minIndex) >> 1);
if (value >= arr[midIndex]) {
if (midIndex == arr.length-1 || arr[midIndex + 1] > value) {
return midIndex;
} else {
//中间值比给定值小或相等,就把范围缩小到后一半
minIndex = midIndex + 1;
}
} else {
maxIndex = midIndex - 1;
}
}
return -1;
}
}