package Search_zhu;
/*
二分查找:依赖的是有序数组
时间复杂度为O(logn)
*/
public class MidSearch {
public static int midsearch(int[] a,int value){
int begin = 0;
int end = a.length - 1;
while (begin <= end){
int mid = 2 * begin + (end - begin) / 2;
if (a[mid] > value){
end = mid - 1;
}else if(a[mid] < value){
begin = mid + 1;
}else{
return mid;
}
}
return -1;
}
public static void main(String[] args) {
int[] a = new int[]{3,5,9,12,39,50};
System.out.println(midsearch(a,9));
}
}
package Search_zhu;
/*
递归实现二分查找:替换for循环
*/
public class DgMidSearch {
public static int midsearch(int[] a,int n,int value){
return dgmidsearch(a,0,n-1,value);
}
public static int dgmidsearch(int[] a,int begin,int end,int value){
if (begin > end) return -1;
int mid = begin + (end - begin) / 2;
if (a[mid] == value){
return mid;
}else if (a[mid] > value){
return dgmidsearch(a,begin,mid - 1,value);
}else{
return dgmidsearch(a,mid + 1,end,value);
}
}
public static void main(String[] args) {
int[] a = new int[]{2,6,9,12,25,35,98};
System.out.println(midsearch(a,7,12));
}
}
package Search_zhu;
/*
二分查找:查找第一个值等于给定值的下标
*/
public class MidSearch1 {
public static int midsearch1(int[] a,int value){
int begin = 0;
int end = a.length - 1;
while (begin <= end){
int mid = begin + (end - begin) / 2;
if (a[mid] > value){
end = mid - 1;
}else if (a[mid] < value){
begin = mid + 1;
}else{
//若mid是第一个或mid的前一个不等于value,直接返回mid;否则end往前推
if ((mid == 0) || a[mid - 1] != value) return mid;
else end = mid - 1;
}
}
return -1;
}
public static void main(String[] args) {
int[] a = new int[]{2,5,9,23,23,36,58};
System.out.println(midsearch1(a,23));
}
}
package Search_zhu;
/*
二分查找:找到最后一个值等于给定值的下标
*/
public class MidSearch2 {
public static int midsearch2(int[] a,int value){
int begin = 0;
int end = a.length - 1;
while (begin <= end){
int mid = begin + (end - begin) / 2;
if (a[mid] > value){
end = mid - 1;
}else if(a[mid] < value){
begin = mid + 1;
}else{
if (mid == end || a[mid + 1] != value){
return mid;
}else{
begin = mid + 1;
}
}
}
return -1;
}
public static void main(String[] args) {
int[] a = new int[]{2,5,23,23,23,36,58};
System.out.println(midsearch2(a,23));
}
}
package Search_zhu;
/*
二分查找:查找第一个大于等于给定值的元素
*/
public class MidSearch3 {
public static int midsearch3(int[] a,int value){
int begin = 0;
int end = a.length - 1;
while (begin <= end){
int mid = begin + (end - begin) / 2;
if (a[mid] >= value){
if (mid == 0 || a[mid - 1] < value ) return mid;
else end = mid - 1;
}else{
begin = mid + 1;
}
}
return -1;
}
public static void main(String[] args) {
int[] a = new int[]{2,5,23,23,23,36,58};
System.out.println(midsearch3(a,5));
}
}
package Search_zhu;
/*
二分查找:查找最后一个小于等于给定值的下标
*/
public class MidSearch4 {
public static int midsearch4(int[] a,int value){
int begin = 0;
int end = a.length - 1;
while (begin <= end){
int mid = begin + (end - begin) / 2;
if (a[mid] > value){
end = mid - 1;
}else{
if (mid == end - 1 || a[mid + 1] > value) return mid;
else begin = mid + 1;
}
}
return -1;
}
public static void main(String[] args) {
int[] a = new int[]{2,5,23,23,23,36,58};
System.out.println(midsearch4(a,25));
}
}