欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++實(shí)現(xiàn)旋轉(zhuǎn)數(shù)組的二分查找

 更新時(shí)間:2014年09月18日 09:24:14   投稿:shichen2014  
這篇文章主要介紹了C++實(shí)現(xiàn)旋轉(zhuǎn)數(shù)組的二分查找方法,涉及數(shù)組的操作,有值得借鑒的技巧,需要的朋友可以參考下

本文實(shí)例講述了C++實(shí)現(xiàn)旋轉(zhuǎn)數(shù)組的二分查找方法,分享給大家供大家參考。具體方法如下:

題目要求:

旋轉(zhuǎn)數(shù)組,如{3, 4, 5, 1, 2}是{1, 2, 3, 4, 5}的一個(gè)旋轉(zhuǎn),要求利用二分查找查找里面的數(shù)。

這是一道很有意思的題目,容易考慮不周全。這里給出如下解決方法:

#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++算法設(shè)計(jì)的學(xué)習(xí)有所幫助。

相關(guān)文章

  • Matlab實(shí)現(xiàn)黑洞優(yōu)化算法的示例代碼

    Matlab實(shí)現(xiàn)黑洞優(yōu)化算法的示例代碼

    根據(jù)黑洞現(xiàn)象原理首次提出BH 算法,它在傳統(tǒng)PSO基礎(chǔ)上引入了新的機(jī)制,有效地提高了收斂速度并防止了陷入局部極值的情況發(fā)生.本文將用Matlab實(shí)現(xiàn)這一算法,需要的可以參考一下
    2022-06-06
  • C++詳解如何實(shí)現(xiàn)單鏈表

    C++詳解如何實(shí)現(xiàn)單鏈表

    線性表的鏈?zhǔn)酱鎯?chǔ)又稱為單鏈表,它是指通過一組任意的存儲(chǔ)單元來存儲(chǔ)線性表中的數(shù)據(jù)元素。本文將用C++實(shí)現(xiàn)單鏈表,需要的可以參考一下
    2022-06-06
  • C語言示例講解do?while循環(huán)語句的用法

    C語言示例講解do?while循環(huán)語句的用法

    在不少實(shí)際問題中有許多具有規(guī)律性的重復(fù)操作,因此在程序中就需要重復(fù)執(zhí)行某些語句。一組被重復(fù)執(zhí)行的語句稱之為循環(huán)體,能否繼續(xù)重復(fù),決定循環(huán)的終止條件
    2022-06-06
  • C++中this指針用法詳解及實(shí)例

    C++中this指針用法詳解及實(shí)例

    這篇文章主要介紹了C++中this指針用法詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下
    2017-04-04
  • c語言中位字段與結(jié)構(gòu)聯(lián)合的組合使用詳解

    c語言中位字段與結(jié)構(gòu)聯(lián)合的組合使用詳解

    本篇文章是對c語言中位字段與結(jié)構(gòu)聯(lián)合的組合使用進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05
  • C++淺析函數(shù)重載是什么

    C++淺析函數(shù)重載是什么

    C++?允許多個(gè)函數(shù)擁有相同的名字,只要它們的參數(shù)列表不同就可以,這就是函數(shù)的重載(Function?Overloading),借助重載,一個(gè)函數(shù)名可以有多種用途
    2022-08-08
  • C++20中的span容器及用法小結(jié)

    C++20中的span容器及用法小結(jié)

    std::span 是一個(gè)非常實(shí)用的工具,可以方便地對數(shù)據(jù)進(jìn)行訪問和處理,同時(shí)也可以提高代碼的可讀性、可維護(hù)性和安全性,這篇文章主要介紹了C++20中的span容器,需要的朋友可以參考下
    2023-03-03
  • C語言實(shí)現(xiàn)靜態(tài)版通訊錄的代碼分享

    C語言實(shí)現(xiàn)靜態(tài)版通訊錄的代碼分享

    這篇文章主要為大家詳細(xì)介紹了如何利用C語言實(shí)現(xiàn)一個(gè)簡單的靜態(tài)版通訊錄,主要運(yùn)用了結(jié)構(gòu)體,一維數(shù)組,函數(shù),分支與循環(huán)語句等等知識(shí),需要的可以參考一下
    2023-01-01
  • 深入理解atoi()與itoa()函數(shù)的用法

    深入理解atoi()與itoa()函數(shù)的用法

    本篇文章是對atoi()與itoa()函數(shù)的用法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05
  • 淺析c/c++中函數(shù)的參數(shù)傳遞

    淺析c/c++中函數(shù)的參數(shù)傳遞

    c/c++中,函數(shù)可以傳遞的參數(shù)有三種形式,值、引用和指針。以下分別對這三種形式進(jìn)行了介紹,需要的朋友可以過來參考下
    2013-07-07

最新評論