旋转数组查找, 二分查找的递归于非递归实现
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)); } }
相关文章推荐
- 旋转数组查找指定的数
- java动态代理(JDK和cglib)
- Effective Matlab—编写高效的Matlab代码
- textarea全屏显示(jquery.textareafullscreen.js插件)
- Arduino 2048 小游戏得到 Banzi 大叔的赐福:It's funny!
- 国防电子代表队荣获2014年成都市百万职工技能大赛计算机程序员决赛团体第一名
- 数据交换格式XML和JSON对比
- 2015062209 - 一天一本书的价值在哪里?
- js 实现图片预加载 (js操作 Image对象属性complete ,事件onload 异步加载图片)
- 校友聊NABCD需求分析
- 第一阶段冲刺站立会议报告——9
- 1-2-3-4-5-6-7-8-9-10的意思
- 百度_谷歌_雅虎搜索引擎URL参数全解析
- 详解树状数组三种模型
- Winsock例子代码:初始化、阻塞/非阻塞、超时设置、select等
- IOS引用私有private framework API 图解
- 学习OpenCV——Kalman滤波
- 上传图片文件
- 梦断代码阅读笔记1
- 二维数组中最大连通子数组