Rotate the minimum element of an array

Source: Internet
Author: User


Description:
Moving the first several elements of an array to the end of an array is called the rotation of an array. Input a rotation of an incremental sorting array and output the smallest element of the rotating array. For example, if an array {3, 4, 5, 1, 2} is a rotation of {1, 2, 3, 4, 5}, the minimum value of this array is 1.
Input:
The input may contain multiple test examples. For each test case,
The first input behavior is an integer N (1 <=n <= 1000000): represents the number of elements in the rotated array.
The second line of the input contains N integers. The range of each integer a is (1 <= A <= 10000000 ).
Output:
Corresponding to each test case,
The smallest element in the output rotating array.
Sample input:
5
3 4 5 1 2
Sample output:
1

Solution:
Method 1: Perform sequential search to find the smallest element. The time complexity is O (n), but the space complexity is O (1). You do not need to open up space for storing arrays. This method can also be accepted.
Method 2: by using the properties of the rotated array and binary search, the first half and the second half are incremental and the second half is smaller than the first half. The main idea is,

While (low! = High-1) {mid = (low + high)/2; if (a [low] <= A [Mid]) {// If the element that low points to is smaller than the element that the mid points to, it means that [low, mid] is an incremental array, the minimum value to be searched is not within this interval low = mid;} else {// The data to be searched is between low and Mid high = mid ;}}

However, there are some special cases to consider about this issue,
Case 1: The array is incremental and the conditions are used.A[0]<A[n-1]The smallest element isA[0].
Case 2: There are equal elements in the array, and there are many processing methods, such as for the sequence1 0 1 1 1And Sequence1 1 1 0 1If the two methods are used for processing, errors will occur.A[low] <= A[mid]However, different low and high conversion methods are required. Therefore, in this case, we need to consider using the sequential search method.

The Code is as follows:

# Include <stdio. h> # include <stdlib. h> void binarysearch (); void sequentialsearch (); int main () {/* binarysearch (); */sequentialsearch (); Return 0;} void sequentialsearch () {int N; int min; int value; while (scanf ("% d", & N )! = EOF) {min = 10000001; For (INT I = 0; I <n; I ++) {scanf ("% d", & value ); if (value <min) {min = value ;}} printf ("% d \ n", min) ;}} void binarysearch () {int N; int * rotatedarray; int low, high, mid; while (scanf ("% d", & N )! = EOF) {rotatedarray = (int *) malloc (sizeof (INT) * n); For (INT I = 0; I <n; I ++) {scanf ("% d", & rotatedarray [I]);} low = 0; high = n-1; If (n = 1) {printf ("% d \ n", rotatedarray [0]); continue;} If (rotatedarray [0] <rotatedarray [n-1]) {// when the sequence is incremented, the minimum value of the first element is printf ("% d \ n", rotatedarray [0]); continue;} // for sequence 1 1 1 0 1 and sequence 1 0 1 1 1, if an error occurs according to the following processing method, that is, the low and high change methods are different in the two cases. // Therefore, when both low, mid, and high are equal, sequential search int min = 10000001; Int minindex = 0; Mid = (high + low)/2; If (rotatedarray [low] = rotatedarray [Mid] & rotatedarray [Mid] = rotatedarray [High]) {for (INT I = low; I <= high; I ++) {If (rotatedarray [I] <min) {min = rotatedarray [I]; minindex = I ;}} printf ("% d \ n", rotatedarray [minindex]); Continue ;}while (low! = (High-1) {mid = (low + high)/2; If (rotatedarray [low] <= rotatedarray [Mid]) {LOW = mid ;} else {high = mid ;}} printf ("% d \ n", rotatedarray [High]) ;}} /*************************************** * *********************** problem: 1386 User: jingxmu language: C ++ result: accepted time: 660 MS memory: 1020 kb ************************************** * *************************/binary search results are as follows: /*************************************** * *********************** problem: 1386 User: jingxmu language: C ++ result: accepted time: 650 MS memory: 4928 kb ************************************** **************************/

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.