您的位置:首页 > 其它

旋转数组查找, 二分查找的递归于非递归实现

2015-06-22 22:46 656 查看
package java_study.JianZhiOffer;

import org.junit.Test;

import java.util.Arrays;

/**
* Created by ethan on 2015/6/22.
* 剑指offer 第八题 旋转数组查找
* 查找算法中重要的:二分,hash,二叉搜索树
* 排序中重要的算法:快排的partition思路(一次可以定位一个值), 堆排序(选出最小的or最大的几个), 归并排序(n个有序子序列)
* 插入排序:保证前n位是有序的, 希尔排序:间隔有序子序列, 冒泡排序: 能挑出最大or最小的几位数放到最后,但是效率比堆排序低, 选择排序:找出最大or最小放在最前or最后
*/
public class No8RotateTheMini {
// 原始的二分查找
public boolean binaryFind(int[] arr, int goal){
return binaryFindImpl(arr, goal, 0, arr.length-1);
}
// 递归实现
public boolean binaryFindImpl(int[] arr, int goal, int first, int last){
if (arr==null) return false;
if (first<0 || last<0 || first>last) return false;
int mid = (first+last)/2;
if (arr[mid]==goal) return true;
else if (arr[mid]<goal) return binaryFindImpl(arr, goal, mid + 1, last);
else return binaryFindImpl(arr, goal, first, mid - 1);
}
// 非递归实现
public boolean binaryFindNoRecursive(int[] arr, int goal){
if (arr==null) return false;
int first = 0;
int end = arr.length-1;
while (first<=end){
int mid = (first+end)/2;
if (arr[mid]==goal)
return true;
else if(arr[mid] < goal)
first = mid+1;
else
end = mid-1;
}
return false;
}
// 对于旋转数组
public int binaryFindRotation(int[] arr, int goal){
return binaryFindRotationInpl(arr, goal, 0, arr.length-1);
}
public int binaryFindRotationInpl(int[] arr, int goal, int first, int end){
if (arr==null) return -1;
if (first<0 || end<0 || first>end) return -1;
int mid = (first+end)/2;
if (arr[mid] == goal) return mid;
else {
if (arr[mid] > arr[first]){
if (arr[mid]>goal && arr[first]<=goal) return binaryFindRotationInpl(arr, goal, first, mid - 1);
else return binaryFindRotationInpl(arr, goal, mid+1, end);
}else {
if (arr[mid]<goal && arr[end]>=goal) return binaryFindRotationInpl(arr, goal, mid+1, end);
else return binaryFindRotationInpl(arr, goal, first, mid-1);
}
}
}
// 旋转数组找出最小的数
public int findRotationSortedArray(int[] arr){
return findRotationSortedArrayImpl(arr,  0, arr.length-1);
}
public int findRotationSortedArrayImpl(int[] arr, int first, int last){
if (arr==null || first<0 || last<0 || first>last) return -1;
if (last-first==1)
return last;
int mid = (first+last)/2;
if (arr[first] < arr[mid]) return findRotationSortedArrayImpl(arr,  mid, last);
else return findRotationSortedArrayImpl(arr, first, mid);
}

@Test
public void test(){
int[] arr = {23, 22, 81, 33, 67, 54, 92, 10, 3};
Arrays.sort(arr);
System.out.println(binaryFind(arr, 81));
System.out.println(binaryFindNoRecursive(arr, 81));
int[] arr1 = {10, 29, 32, 35, 42, 50, -1, 1, 5, 8, 9};
System.out.println(binaryFindRotation(arr1, 31));
System.out.println(findRotationSortedArray(arr1));
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息