public class MinElementInRotatedArrayUsingRecursion {
public static void main(String[] args) {
int arr1[] = { 5, 6, 1, 2, 3, 4 };
int n1 = arr1.length;
System.out.println("The minimum element is " + findMin(arr1));
int arr2[] = { 1, 2, 3, 4 };
int n2 = arr2.length;
System.out.println("The minimum element is " + findMin(arr2));
int arr3[] = { 1 };
int n3 = arr3.length;
System.out.println("The minimum element is " + findMin(arr3));
int arr4[] = { 1, 2 };
int n4 = arr4.length;
System.out.println("The minimum element is " + findMin(arr4));
int arr5[] = { 2, 1 };
int n5 = arr5.length;
System.out.println("The minimum element is " + findMin(arr5));
int arr6[] = { 5, 6, 7, 1, 2, 3, 4 };
int n6 = arr6.length;
System.out.println("The minimum element is " + findMin(arr6));
int arr7[] = { 1, 2, 3, 4, 5, 6, 7 };
int n7 = arr7.length;
System.out.println("The minimum element is " + findMin(arr7));
int arr8[] = { 2, 3, 4, 5, 6, 7, 8, 1 };
int n8 = arr8.length;
System.out.println("The minimum element is " + findMin(arr8));
int arr9[] = { 3, 4, 5, 1, 2 };
int n9 = arr9.length;
System.out.println("The minimum element is " + findMin(arr9));
}// main
public static int findMin(int[] num) {
return findMin(num, 0, num.length - 1);
}
public static int findMin(int[] num, int start, int last) {
// only 1 element
if (start == last)
return num[start];
// only 2 elements
if ((last - start) == 1)
return Math.min(num[start], num[last]);
int middle = start + (last - start) / 2;
// not rotated
if (num[start] < num[last]) {
return num[start];
}
// go last side
/*
* int[] a = 2,3,4,5,1
* a[start]=2 and a[last]=1 and a[mid] = 4
* a[mid] > a[start]
* Then, as the array is rotated, That's why, calculation will start from
* a[middle] to a[last]
*/
else if (num[middle] > num[start]) {
return findMin(num, middle, last);
} else {
// go start side
/*
* int[] a = 4,5,1,2,3
* a[start]= 4 and a[last]=3 and a[mid] = 1
* a[mid] < a[start]
* Then, as the array is rotated, That's why, calculation will start from
* a[start] to a[middle]
*/
return findMin(num, start, middle);
}
}
}
迭代方法:
public class MinElementInRotatedArrayUsingRecursionAndIteration {
public static void main(String[] args) {
int arr1[] = { 5, 6, 1, 2, 3, 4 };
int n1 = arr1.length;
System.out.println("The minimum element is " + findMin(arr1));
int arr2[] = { 1, 2, 3, 4 };
int n2 = arr2.length;
System.out.println("The minimum element is " + findMin(arr2));
int arr3[] = { 1 };
int n3 = arr3.length;
System.out.println("The minimum element is " + findMin(arr3));
int arr4[] = { 1, 2 };
int n4 = arr4.length;
System.out.println("The minimum element is " + findMin(arr4));
int arr5[] = { 2, 1 };
int n5 = arr5.length;
System.out.println("The minimum element is " + findMin(arr5));
int arr6[] = { 5, 6, 7, 1, 2, 3, 4 };
int n6 = arr6.length;
System.out.println("The minimum element is " + findMin(arr6));
int arr7[] = { 1, 2, 3, 4, 5, 6, 7 };
int n7 = arr7.length;
System.out.println("The minimum element is " + findMin(arr7));
int arr8[] = { 2, 3, 4, 5, 6, 7, 8, 1 };
int n8 = arr8.length;
System.out.println("The minimum element is " + findMin(arr8));
int arr9[] = { 3, 4, 5, 1, 2 };
int n9 = arr9.length;
System.out.println("The minimum element is " + findMin(arr9));
}// main
// iterative method
public static int findMin(int[] nums) {
int start =0;
int last =nums.length-1;
while(start <= last){
if(nums[start]<=nums[last]) return nums[start];
int mid =(start + last)/2;
/* int[] a = 2,3,4,5,1
a[start]=2 and a[last]=1
a[mid] = 4
a[mid] > a[start]
Then, as the array is rotated, That's why, calculation will start from a[middle] to a[last]
*/
if(nums[mid]>=nums[start]){
start = mid +1;
}else{
/* int[] a = 4,5,1,2,3
a[start]= 4 and a[last]=3
a[mid] = 1
a[mid] < a[start]
Then, as the array is rotated, That's why, calculation will start from a[start] to a[middle]
*/
last = mid;
}
}
return -1;
}
}