Find and sort are the algorithms that are often used in programs
Look for: sequential lookups, binary lookups, hash table lookups, and two-tree sorting lookups.
The focus of a hash table and a two-tree lookup is on its data structure. The main advantage of a hash table is the ability to find an element at an O (1) time, which is the most efficient way to find it. The disadvantage is that additional space is required to implement the hash table.
Sorting is divided into insert sort, bubble sort, recursive sort, quick sort etc. The pros and cons of these sorts of methods (extra space consumption, average time complexity, and worst-time complexity), are characterized by emphasis.
Quick Sort
The key to fast sorting is to first select a number in the array, then the number in the array is divided into two parts, smaller than the selection of the array on the left side, large to the right.
Topic:
Minimum number of rotated array
Move a number of elements at the beginning of an array to the end of the array, enter a rotation of an incrementally sorted array, and output the smallest element of the rotated number.
Example: {2,3,4,0,1} is a rotation of {0,1,2,3,4}, the minimum value of this array is 0
Procedure 1.0
The failed program, traversing from the beginning, Time complexity O (N), this idea does not take advantage of the characteristics of the input rotation array, casually implemented but failed
int Minnum (int* array, int length) {int cur = array[0];for (int i = 1; i < length; i++) {if (array[i]<cur) {cur = array [i];}} return cur;}
Procedure 2.0
method is similar to binary lookup
int Minnum (int* array, int length) {assert (array); ASSERT (length>0); int left = 0;int right = Length-1;int mid = left;//is initialized to 0 so that if 0 elements are rotated, they are returned directly while (Array[left] >= arr Ay[right])//array is rotated after the array {if (Right-left = = 1) {mid = Right;break;} Mid = (left + right)/2;if (Array[mid] >= Array[left]) {left = mid;} else if (Array[mid] <= array[right]) {right = mid;}} return array[mid];} At the first detection, it was found that how to run is the value of the first element returned, and later detected that it was accidentally added an int in the while loop block when defining mid, which makes the mid a local variable of this block, which is discarded after the loop.
Procedure 3.0
In 2.0, there is an imperfect place, if the array is {1,0,1,1,1} or {1,1,1,0,1}, its left and right and the middle are 1, unable to move two pointers to narrow the search, so have to take the method of sequential lookup
Int mininorder (int *array,int left,int right) {int ret = array[left];for (int i = left + 1; i <= right; i++) {if (ret> Array[i]) {ret = array[i];}} Return ret;} Int minnum (int* array, int length) {assert (array); assert (length > 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] <= array[right]) {right = mid;}} Return array[mid];}
Test
void Test () {int a[10] = {8,9,10,11,1,2,3,4,5,6};cout<<minnum (A, 10);} void Test1 () {int a[5] = {1, 0, 1, 1, 1};cout << Minnum (A, 5);}
This article is from the "incomparable Warm yang" blog, please be sure to keep this source http://10797127.blog.51cto.com/10787127/1772443
About finding and sorting