Analysis of the minimum numeric algorithm for rotating arrays and implementation of complete C language code

Source: Internet
Author: User

Start by understanding what a rotating array is: Moving several elements at the beginning of an array to the end of the array, becoming a rotating array, such as an array of {3,7,1,8,2} {1,8,2,3,7}.

Title: Enter a rotation of an incrementally sorted array, outputting the smallest element of the rotated array, such as a rotated array {3,4,5,1,2} of the {1,2,3,4,5} array, with a minimum element of 1,

Parsing: After seeing the topic, we may first think of traversing this array, we can find the smallest element, time complexity is O (n), but this idea obviously does not take advantage of the array is an ascending sort of character, so we have to continue to look for better methods, because the original array is ascending sort, Then the rotated array is composed of two incrementally sorted arrays, and the elements of the preceding sub-array are greater than or equal to the elements of the post-face array, and the smallest element is exactly separating the two sub-arrays, so we can try to find the smallest element with a binary search idea.

As with the binary lookup method, we can point to the first element and the last element of the array with two pointers, according to the rules of rotation, the first element should be greater than or equal to the last element, (this is not entirely true, there is a case is special, discussed later),

Then we can find the element in the middle of the array, if the array is in the preceding increment sub-array, then he should be greater than or equal to the first pointer to the element, the smallest element of the array should be at the back of the middle element, so we can point the pointer to the middle element, thus narrowing the scope of the lookup, Similarly, if the intermediate element is in the latter increment sub-array, then he should be less than or equal to the element pointed to by the second pointer, at which point the smallest element in the array should precede the intermediate element, so we can point the second pointer to the middle element, narrowing the lookup.

In the thought above, the first pointer always points to the element that increments the array, the second pointer points to the element that increments the array, and the first pointer points to the last element of the incremented array, while the second pointer points to the first element of the back-face array, and the second pointer points to the smallest element, ending with a loop.

The first element mentioned above should be greater than or equal to the last element, special case: If the first 0 elements of the sorted array are moved to the last surface, that is, the array itself, which is still the rotation of the array, so the first number is the smallest number, we can directly return,

There is another case where the array {1,0,1,1,1} and the group {1,1,1,0,1} can be seen as the rotation of the incrementing sorted array {0,1,1,1,1}, so that the first element, the middle element, and the last element are all 1, We cannot tell if the median number 1 is a preceding increment or a second increment, so we can not use the idea of a binary lookup, only using the sequential lookup method.

The specific code is as follows:

<span style= "FONT-SIZE:24PX;" >//the smallest element of the rotated array #include<stdio.h>int minorder (int a[],int index,int index2); int min (int *a,int length) {int index , Index2,mid;if (a==null| |     Length<=0) return 0; index=0; Index2=length-1;   Mid=index; When the first 0 elements are moved to the back, that is, the sorted array itself, you can return directly midwhile (A[index]>=a[index2]) {if (index2-index==1)//Two pointers to the elements adjacent to each other,          That is, the element that Index2 points to is the smallest element {mid=index2;  Break  } mid= (INDEX+INDEX2)/2; if (A[index]==a[index2] && A[mid]==a[index])//If the intermediate element is not judged to be the first increment element or the second increment element, then the order looks for {return Minorder (A,index,  INDEX2);  } if (A[mid]>=a[index]) Index=mid;   if (A[mid]<=a[index2]) Index2=mid;} return a[mid];}   int minorder (int a[],int index,int index2) {int result=a[index];   int i;   for (i=index+1;i<=index2;i++) {if (Result>a[i])//Order lookup comparison, find the smallest element result=a[i]; } return result;  int main () {int a[5];  int i,x;  for (i=0;i<5;i++) {scanf ("%d", &a[i]);  } x=min (a,5);  printf ("%d\n", X); return 0;} </span> 



Analysis of the minimum numeric algorithm for rotating arrays and implementation of complete C language code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.