Find Minimum in Rotated Sorted Array, rotatedsorted
This article is in the study of the summary, welcome to reprint but please note the Source: http://blog.csdn.net/pistolove/article/details/43416613
Suppose a sorted array is rotated at some unknown to you beforehand.
(I. e .,0 1 2 4 5 6 7
Might become4 5 6 7 0 1 2
).
Find the minimum element.
You may assume no duplicate exists in the array.
Ideas:
(1) The question is to give an ordered integer array. The first m (m> 0) elements of the array are moved to the back of the array to form a new array, the minimum element in the new array.
(2) This question examines the array traversal problem. This question is also relatively simple. The solution is simple and violent. You can sort the question directly using the method in the class library to obtain the minimum element (the following algorithm is attached with Arrays. the implementation of the Sorting Algorithm in sort (). If you are interested, you can take a look at it and feel that it is complicated to write.) The second solution is the "general solution". You can obtain the minimum value through traversing from the front and back, the efficiency from the back to the back is higher than that from the back to the back. solution 3 should be "efficient", but it has not been provided here, the idea of speculation is to seek the minimum value through a method similar to "binary search". If you are interested, you can study it.
(3) I hope this article will help you.
The algorithm code is implemented as follows:
/*** @ Author liqq * solution 1: simple brute force */public int findMin (int [] num) {if (num = null | num. length = 0) return 0; Arrays. sort (num); return num [0];}
/***** @ Author liqq append Arrays. sort () source code for reference */public int findMin (int [] num) {if (num = null | num. length = 0) return 0; sort (num, 0, num. length); return num [0];} private static void sort (int x [], int off, int len) {// Insertion sort on smallest arraysif (len <7) {for (int I = off; I <len + off; I ++) for (int j = I; j> off & x [j-1]> x [j]; j --) swap (x, j, j-1); return;} // Choose a partition ele Ment, vint m = off + (len> 1); // Small arrays, middle elementif (len> 7) {int l = off; int n = off + len-1; if (len> 40) {// Big arrays, pseudo domedian of 9int s = len/8; l = med3 (x, l, l + s, l + 2 * s); m = med3 (x, m-s, m, m + s); n = med3 (x, n-2 * s, n-s, n);} m = med3 (x, l, m, n); // Mid-size, med of 3} int v = x [m]; // Establish Invariant: v * (<v) * (> v) * v * int a = off, B = a, c = off + Len-1, d = c; while (true) {while (B <= c & x [B] <= v) {if (x [B] = v) swap (x, a ++, B); B ++;} while (c> = B & x [c]> = v) {if (x [c] = v) swap (x, c, d --); c --;} if (B> c) break; swap (x, B ++, c --);} // Swap partition elements back to middleint s, n = off + len; s = Math. min (a-off, B-a); vecswap (x, off, B-s, s); s = Math. min (d-c, n-d-1); vecswap (x, B, n-s, s); // Recursively sort non-partition-el Ementsif (s = B-a)> 1) sort (x, off, s); if (s = d-c)> 1) sort (x, n-s, s);} private static void swap (int x [], int a, int B) {int t = x [a]; x [a] = x [B]; x [B] = t;} private static int med3 (int x [], int a, int B, int c) {return (x [a] <x [B]? (X [B] <x [c]? B: x [a] <x [c]? C: a): (x [B]> x [c]? B: x [a]> x [c]? C: a);} private static void vecswap (int x [], int a, int B, int n) {for (int I = 0; I <n; I ++, a ++, B ++) swap (x, a, B );}
/*** @ Author liqq * solution 2 the efficiency of traversing from the front and back is slightly higher */public int findMinFromHead (int [] num) {if (num = null | num. length = 0) return 0; int len = num. length; int leftHalfMin = num [0]; for (int I = 1; I <len; I ++) {if (leftHalfMin> = num [I]) {leftHalfMin = num [I] ;}return leftHalfMin;} public int findMinFromEnd (int [] num) {if (num = null | num. length = 0) return 0; if (num. length = 1) return num [0]; int len = num. length; int min = num [len-1]; for (int I = len-2; I> = 0; I --) {if (min <= num [I]) {return min;} else {min = num [I]; if (I = 0) {return min ;}} return-1 ;}