"Written test" 26, minimum number of rotated array

Source: Internet
Author: User

Moves the first element of an array to the end of the array, which we call the rotation of the array. Enter a rotation of an incrementally sorted array, outputting the smallest element of the rotated array. For example, the array {3,4,5,1,2} is a rotation of {1,2,3,4,5}, and the minimum value of the array is 1


This problem can be found with two points of thought:

The time complexity of binary lookup is: O (LOGN)


/** * Title: Move a number of elements at the beginning of an array to the end of the array, which we call the rotation of the array. Enter a rotation of an incrementally sorted array, * output the smallest element of the rotated array. For example, the array {3,4,5,1,2} is a rotation of {1,2,3,4,5}, and the minimum value of the array is 1 * time: August 27, 2015 10:40:25 * file: Min.java *cutter_point */package Bishi. Offer50.y2015.m08.d27;public class min{/** * Use dichotomy to find the smallest value in a rotated array * @param numbers * @return * @throws Exception */public Stati c int getmin (int numbers[]) throws exception{if (numbers = null) {throw new Exception ("Array is empty");}  Ifint Minnumber = 0;int start = 0;int end = Numbers.length-1;int mid = Start;while (Start < end) {if ((end-start) = = 1) {minnumber = Numbers[end];break;} Ifmid = (start + end)/2;//takes into account when start and end have the same value as the MID3 place//In order to find if (numbers[start] = = Numbers[mid] && numbers[ MID] = = Numbers[end]) {return Minorder (numbers, start, end);} IfIf (Numbers[mid] > Numbers[start]) {start = mid;} Ifelse{end = mid;} Else}//whilereturn Minnumber;} public static int Minorder (int numbers[], int index1, int index2) {int result = Numbers[index1];while (index1 <= index2) { if (Result > numbers[index1]) result = Numbers[index1];++index1;} Whilereturn result;} public static void Main (string[] args) {int array[] = {3,4,5,0,1,1,2,3};int array2[] = {1,0,1,1,1};try{system.out.print ( Getmin (array));} catch (Exception e) {e.printstacktrace ();}}}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"Written test" 26, minimum number of rotated array

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.