How to solve the interview questions of finding and sorting in C++
This article will explain in detail how to solve the interview questions found and sorted in C++. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
Title:
The minimum number of rotated array
Move the first elements of an array to the end of the array, enter a rotation of an incrementally sorted array, and output the smallest element of the rotation number.
For example, {2pyrrine 3pyrr4 0pr 1} is the rotation of {0pr 1pm 2je 3p4}, and the minimum value of this array is 0.
Program 1.0
The failed program, traversing from the beginning, has a time complexity of O (N). This idea does not make use of the characteristics of the input rotation array and is casually implemented but failed.
Int MinNum (int* array, int length) {int cur = array [0]; for (int I = 1; I
< length; i++) { if (array[i]0); int left = 0; int right = length - 1; int mid = left;//初始化为0,这样若旋转了0个元素则直接返回 while (array[left] >= array [right]) / / the array is rotated {if (right-left = = 1) {mid = right; break;} mid = (left + right) / 2 If (array [mid] > = array [left]) {left = mid;} else if (array [mid] 0); int left = 0; int right = length-1; int mid = left While (array [left] > = array [right]) {if (right-left = = 1) {mid = right; break;} mid = (left + right) / 2 If (array [left] = = array [right] & & array [left] = = array [mid]) {return MinInOrder (array, left, right);} if (array [mid] > = array [left]) {left = mid } else if (array [mid]