旋转数组的二分查找
2012-07-15 13:11
218 查看
题目:旋转数组,如{3, 4, 5, 1, 2}是{1, 2, 3, 4, 5}的一个旋转,利用二分查找查找里面的数
这是一道很有意思的题目,容易考虑不周全
这是一道很有意思的题目,容易考虑不周全
#include <iostream> using namespace std; int sequentialSearch(int *array, int size, int destValue) { int pos = -1; if (array == NULL || size <= 0) return pos; for (int i = 0; i < size; i++) { if (array[i] == destValue) { pos = i; break; } } return pos; } int normalBinarySearch(int *array, int leftPos, int rightPos, int destValue) { int destPos = -1; if (array == NULL || leftPos < 0 || rightPos < 0) { return destPos; } int left = leftPos; int right = rightPos; while (left <= right) { int mid = (right - left) / 2 + left; if (array[mid] == destValue) { destPos = mid; break; } else if (array[mid] < destValue) { left = mid + 1; } else { right = mid - 1; } } return destPos; } int rotateBinarySearch(int *array, int size, int destValue) { int destPos = -1; if (array == NULL || size <= 0) { return destPos; } int leftPos = 0; int rightPos = size - 1; while (leftPos <= rightPos) { if (array[leftPos] < array[rightPos]) { destPos = normalBinarySearch(array, leftPos, rightPos, destValue); break; } int midPos = (rightPos - leftPos) / 2 + leftPos; if (array[leftPos] == array[midPos] && array[midPos] == array[rightPos]) { destPos = sequentialSearch(array, size, destValue); break; } if (array[midPos] == destValue) { destPos = midPos; break; } if (array[midPos] >= array[leftPos]) { if (destValue >= array[leftPos]) { destPos = normalBinarySearch(array, leftPos, midPos - 1, destValue); break; } else { leftPos = midPos + 1; } } else { if (array[midPos] <= array[rightPos]) { destPos = normalBinarySearch(array, midPos + 1, rightPos, destValue); break; } else { rightPos = midPos - 1; } } } return destPos; } int main() { //int array[] = {3, 4, 5, 1, 2}; //int array[] = {1, 2, 3, 4, 5}; //int array[] = {1, 0, 1, 1, 1}; //int array[] = {1, 1, 1, 0, 1}; //int array[] = {1}; //int array[] = {1, 2}; int array[] = {2, 1}; const int size = sizeof array / sizeof *array; for (int i = 0; i <= size; i++) { int pos = rotateBinarySearch(array, size, array[i]); cout << "find " << array[i] << " at: " << pos + 1 << endl; } for (int i = size; i >= 0; i--) { int pos = rotateBinarySearch(array, size, array[i]); cout << "find " << array[i] << " at: " << pos + 1 << endl; } }
相关文章推荐
- 剑指Offer面试题8旋转数组的最小数字(二分查找)附带快排和按年龄排序
- leetcode 81. Search in Rotated Sorted Array II-旋转数组|二分查找
- LeetCode-Find Minimum in Rotated Sorted Array II-旋转排序数组找最小-二分查找
- 微策略面试题:在旋转后的数组中查找元素(二分查找)
- leetcode 153. Find Minimum in Rotated Sorted Array 旋转数组(不重复)+二分查找
- 旋转有序数组的二分查找
- Search in Rotated Sorted Array 在旋转过的有序数组中进行二分查找 重点看解法二 三(重重)
- 旋转数组的二分查找
- leetcode 81. Search in Rotated Sorted Array II 旋转数组查找+二分查找
- 二分查找与旋转排序数组 算法题
- leetcode 154. Find Minimum in Rotated Sorted Array II 旋转数组(重复)+二分查找
- Search in Rotated Sorted Array II 旋转数组二分查找 有重复
- 旋转数组(二分查找)
- 旋转数组的二分查找
- 【leetcode】有重复的旋转排序数组查找(二分)
- leetcode旋转数组查找 二分查找的变形
- OJ日常 | 二分查找——旋转数组的最小数字
- 循环有序数组/旋转数组的二分查找
- 在一个旋转过的有序数组上实现二分查找 收藏
- 旋转数组的二分查找