C++实现旋转数组的二分查找

 更新时间:2014年09月18日 09:24:14   投稿:shichen2014  
这篇文章主要介绍了C++实现旋转数组的二分查找方法,涉及数组的操作,有值得借鉴的技巧,需要的朋友可以参考下

本文实例讲述了C++实现旋转数组的二分查找方法,分享给大家供大家参考。具体方法如下:

题目要求:

旋转数组,如{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;
 }
}

希望本文所述对大家C++算法设计的学习有所帮助。

相关文章

  • C++ const与constexpr区别小结

    C++ const与constexpr区别小结

    C++11标准中,const用于为修饰的变量添加只读属性,而constexpr关键字则用于指明其后是一个常量,本文主要介绍了C++ const与constexpr区别小结,感兴趣的可以了解一下
    2024-03-03
  • C++调用Python基础功能实例详解

    C++调用Python基础功能实例详解

    c++调用Python首先安装Python,本文以win7为例,给大家详细介绍C++调用Python基础功能,需要的朋友参考下吧
    2017-04-04
  • 浅析C++11中的右值引用、转移语义和完美转发

    浅析C++11中的右值引用、转移语义和完美转发

    对于c++11来说移动语义是一个重要的概念,一直以来我对这个概念都似懂非懂。最近翻翻资料感觉突然开窍,因此顺便记录下C++11中的右值引用、转移语义和完美转发,方便大家查阅参考。
    2016-08-08
  • 将字符串str1复制为字符串str2的三种解决方法

    将字符串str1复制为字符串str2的三种解决方法

    以下是对将字符串str1复制为字符串str2的三种解决方法进行了详细的介绍,需要的朋友可以过来参考下,希望对大家有所帮助
    2013-10-10
  • C++二维数组中的查找算法示例

    C++二维数组中的查找算法示例

    这篇文章主要介绍了C++二维数组中的查找算法,结合实例形式分析了C++二维数组进行查找的原理与具体实现技巧,需要的朋友可以参考下
    2017-05-05
  • C/C++中的回调用法详细讲解

    C/C++中的回调用法详细讲解

    这篇文章主要介绍了回调函数在C/C++中的重要意义及应用,回调函数通过将函数作为参数传递,实现了模块的解耦、灵活性和可扩展性,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2024-12-12
  • 深入解读C++中的右值引用

    深入解读C++中的右值引用

    这里来带大家深入解读C++中的右值引用,右值引用是C++新标准中的重要特性,包括C++11中的引用折叠,首先还是先来看一下右值引用的概念:
    2016-05-05
  • 浅谈QT打包的两种方式

    浅谈QT打包的两种方式

    本文主要介绍了浅谈QT打包的两种方式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-03-03
  • C++begin和end运算符的返回迭代器的类型如何判断?

    C++begin和end运算符的返回迭代器的类型如何判断?

    今天小编就为大家分享一篇关于C++begin和end运算符的返回迭代器的类型如何判断?,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-04-04
  • C/C++程序编译流程详解

    C/C++程序编译流程详解

    C/C++程序编译过程包括下面4个阶段:1.预处理,2.编译,3.汇编,4.链接。下面我们就来详细分析下这几个阶段。
    2016-04-04

最新评论