////////////////////////////////////////////////// ///////////////////////////////////////////
// 12 title VIII: minimum number of rotation of the array
int RotatedBinarySearchMinNum(int aiArray[], int iLen) { int iLeft = 0; int iMid = 0; int iRight = iLen - 1; while (aiArray[iLeft] >= aiArray[iRight]) { if (iRight - iLeft == 1) { iMid = iRight; break; } iMid = iLeft + (iRight - iLeft) / 2; if (aiArray[iMid] >= aiArray[iLeft]) { iLeft = iMid; } else if (aiArray[iMid] <= aiArray[iRight]) { iRight = iMid; } } return aiArray[iMid]; } // 旋转数组中查找 int RotatedBinarySearch(int aiArray[], int iLen, int iTarget) { int iLeft = 0; int iMid = 0; int iRight = iLen - 1; the while (lLeft <= IRight) { IMID = lLeft + (IRight - lLeft) / 2 ; if (aiArray [IMID] == Itarget) { return IMID; } // Case 1: point of rotation to the right of the median if (aiArray [IMID]> = aiArray [lLeft]) { // leftmost element <target = Find <median IF (aiArray [lLeft] <= Itarget && Itarget < aiArray [IMID]) { IRight = IMID - . 1 ; } the else { lLeftIMID + = . 1 ; } } // Case 2: the left rotation point median the else { // median <search target <= rightmost element IF (aiArray [IMID] <Itarget && Itarget <= aiArray [IRight]) { lLeft = IMID + . 1 ; } the else { IRight = IMID - . 1 ; } } } return - . 1 ; } void RotatedBinarySearchTestFunc () { COUT <<"\n\n --------------- RotatedBinarySearchTestFunc Start -------------->" << endl; int aiArray[] = {9, 10, 11, 12, 13, 1, 3, 4, 5, 8}; int iLen = sizeof(aiArray) / sizeof(int); TRAVERSAL_ARRAY(aiArray, iLen); cout << "旋转数组中最小数字: " << RotatedBinarySearchMinNum(aiArray, iLen) << endl; int iFindNum = 5; int iIndex = RotatedBinarySearch(aiArray, iLen, iFindNum); if (iIndex > 0) { printf("Find %02d[%02d] Success!", iFindNum, iIndex); } else { cout << "Find: " << iFindNum << " Failed!" << endl; } cout << "\n\n --------------- RotatedBinarySearchTestFunc Start -------------->" << endl; }