二分查找(2)(数据结构)Java版
嗨!收到一张超级美丽的风景图,愿你每天都能顺心
![](https://img-blog.csdnimg.cn/img_convert/cf33cb8a4e21441f941e603eb1421fde.jpeg)
在看这篇文章前可以先去看看小编写的二分查找(数据结构)Java版这篇文章,会更有理解。
查找数组中有多个相等的元素最左边的元素或最右边的元素
这里是上一篇文章的代码
稍微对数组内的数据做了一下调整
import java.util.Arrays;
public class BinarySearch {
public static void testBinarySearch(int[] a, int target) {
int index = Arrays.binarySearch(a, target);
if (index<0){
System.out.printf("对于目标值 %d, 在数组 %s 中查找不到应该插入的索引位置是 %d\n", target, Arrays.toString(a), Math.abs(index+1));
//插入元素
int InsertIndex = Math.abs(index+1);//插入点索引
int []b = new int[a.length + 1];
System.arraycopy(a,0,b,0,InsertIndex);
b[InsertIndex] = target;
System.arraycopy(a,InsertIndex,b,InsertIndex+1,a.length-InsertIndex);
System.out.printf("插入%d后新数组是:%s\n",target,Arrays.toString(b));
}else {
System.out.printf("对于目标值 %d, 在数组 %s 中的索引是 %d\n", target, Arrays.toString(a), index);
}
}
public static void main(String[] args) {
int[] a = {1, 2, 3, 4, 4, 4, 5, 6, 7};
int target1 = 4;
int target2 = 5;
int target3 = 8;
int target4 = 1;
testBinarySearch(a, target1);
testBinarySearch(a, target2);
testBinarySearch(a, target3);
testBinarySearch(a, target4);
}
}
那如果我们想要查找最左边的4,应该做出哪些改变呢?
首先来整理一下思路:
- 首先一样,i定位索引为0的位置,j定位索引为数组长度-1的位置
- 折半查找中间位置的索引为4,然后记录索引为4的这个位置是否是我们要找的元素4,正好是4,记录这个位置,查找继续,将m-1的值赋值给j
- 继续重复上述操作,如图所示,找到下一个4了再更新记录位置。
- 当i大于j的时候就退出循环了。(由于地方空间位置有限,小编就没有画出来了)
接下来我们找到我们基础版的代码,看看有哪里是需要改动的
private static int binarySearch(int[] a, int fromIndex, int toIndex,
int key) {
int low = fromIndex;
int high = toIndex - 1;
int candidate = -1;
while (low <= high) {
int mid = (low + high) >>> 1;
int midVal = a[mid];
if(midVal < key) {
low = mid + 1;
}
else{
candidate = mid;
high = mid - 1;
}
}
return candidate;
}
然后再加上一个测试方法来输出我们的结果
public static void testBinarySearch(int[] a, int fromIndex, int toIndex,
int key){
int index = binarySearch(a, fromIndex, toIndex, key);
if (index != -1) { // 检查是否存在重复值
System.out.printf("对于要查找的重复值%d,在数组 %s 的索引值为%d\n", key, Arrays.toString(a), index);
} else {
System.out.println("要查找的值未在数组中找到");
}
}
在我们上一篇文章里加入这串方法,用来查询具有重复的元素的最左边的值。下面是总代码
import java.util.Arrays;
public class BinarySearch {
private static int binarySearch(int[] a, int fromIndex, int toIndex,
int key) {
int low = fromIndex;
int high = toIndex - 1;
int candidate = -1;
while (low <= high) {
int mid = (low + high) >>> 1;
int midVal = a[mid];
if(midVal < key) {
low = mid + 1;
}
else{
candidate = mid;
high = mid - 1;
}
}
return candidate;
}
public static void testBinarySearch(int[] a, int fromIndex, int toIndex,
int key){
int index = binarySearch(a, fromIndex, toIndex, key);
if (index != -1) { // 检查是否存在重复值
System.out.printf("对于要查找的重复值%d,在数组 %s 的索引值为%d\n", key, Arrays.toString(a), index);
} else {
System.out.println("要查找的值未在数组中找到");
}
}
public static void testBinarySearch(int[] a, int target) {
int index = Arrays.binarySearch(a, target);
if (index<0){
System.out.printf("对于目标值 %d, 在数组 %s 中查找不到应该插入的索引位置是 %d\n", target, Arrays.toString(a), Math.abs(index+1));
//插入元素
int InsertIndex = Math.abs(index+1);//插入点索引
int []b = new int[a.length + 1];
System.arraycopy(a,0,b,0,InsertIndex);
b[InsertIndex] = target;
System.arraycopy(a,InsertIndex,b,InsertIndex+1,a.length-InsertIndex);
System.out.printf("插入%d后新数组是:%s\n",target,Arrays.toString(b));
}else {
System.out.printf("对于目标值 %d, 在数组 %s 中的索引是 %d\n", target, Arrays.toString(a), index);
}
}
public static void main(String[] args) {
int[] a = {1, 2, 3, 4,4, 4, 5, 6, 7};
int target1 = 4;
int target2 = 5;
int target3 = 8;
int target4 = 1;
testBinarySearch(a,0,a.length-1,4);
testBinarySearch(a, target1);
testBinarySearch(a, target2);
testBinarySearch(a, target3);
testBinarySearch(a, target4);
}
}
既然有查找最左边的重复元素,是不是同理有查找元素最右边的值。
只需要相对与刚刚的代码改动两处即可
接下来来看看总代码
import java.util.Arrays;
public class BinarySearch {
private static int binarySearch(int[] a, int fromIndex, int toIndex,
int key) {
int low = fromIndex;
int high = toIndex - 1;
int candidate = -1;
while (low <= high) {
int mid = (low + high) >>> 1;
int midVal = a[mid];
if(midVal < key) {
low = mid + 1;
}
else{
candidate = mid;
high = mid - 1;
}
}
return candidate;
}
private static int binarySearch1(int[] a, int fromIndex, int toIndex,
int key) {
int low = fromIndex;
int high = toIndex - 1;
int candidate = -1;
while (low <= high) {
int mid = (low + high) >>> 1;
int midVal = a[mid];
if(key < midVal) {
high = mid - 1;
}
else{
candidate = mid;
low = mid + 1;
}
}
return candidate;
}
public static void testBinarySearch(int[] a, int fromIndex, int toIndex,
int key){
int index = binarySearch(a, fromIndex, toIndex, key);
if (index != -1) { // 检查是否存在重复值
System.out.printf("对于要查找的重复值%d,在数组 %s 的索引值为%d\n", key, Arrays.toString(a), index);
} else {
System.out.println("要查找的值未在数组中找到");
}
}
public static void testBinarySearch1(int[] a, int fromIndex, int toIndex,
int key){
int index = binarySearch1(a, fromIndex, toIndex, key);
if (index != a.length) { // 检查是否存在重复值
System.out.printf("对于要查找的重复值%d,在数组 %s 的索引值为%d\n", key, Arrays.toString(a), index);
} else {
System.out.println("要查找的值未在数组中找到");
}
}
public static void testBinarySearch(int[] a, int target) {
int index = Arrays.binarySearch(a, target);
if (index<0){
System.out.printf("对于目标值 %d, 在数组 %s 中查找不到应该插入的索引位置是 %d\n", target, Arrays.toString(a), Math.abs(index+1));
//插入元素
int InsertIndex = Math.abs(index+1);//插入点索引
int []b = new int[a.length + 1];
System.arraycopy(a,0,b,0,InsertIndex);
b[InsertIndex] = target;
System.arraycopy(a,InsertIndex,b,InsertIndex+1,a.length-InsertIndex);
System.out.printf("插入%d后新数组是:%s\n",target,Arrays.toString(b));
}else {
System.out.printf("对于目标值 %d, 在数组 %s 中的索引是 %d\n", target, Arrays.toString(a), index);
}
}
public static void main(String[] args) {
int[] a = {1, 2, 3, 4,4, 4, 5, 6, 7};
int target1 = 4;
int target2 = 5;
int target3 = 8;
int target4 = 1;
testBinarySearch(a,0,a.length-1,4);
testBinarySearch1(a,0,a.length-1,4);
testBinarySearch(a, target1);
testBinarySearch(a, target2);
testBinarySearch(a, target3);
testBinarySearch(a, target4);
}
}
-20-55.png&pos_id=img-vUuuie8n-1718269788685)
结语
本次分享就到这里了,感谢小伙伴的浏览,如果有什么建议,欢迎在评论区留言,如果给小伙伴们带来了一些收获,请留下你的小赞,你的点赞和关注将会成为博主分享每日学习的动力。