Repository navigation
Expand file tree
/
Copy path027_ArrayRotation.java
More file actions
80 lines (62 loc) · 2.41 KB
/
Copy path027_ArrayRotation.java
File metadata and controls
80 lines (62 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Coding Interviews: Questions, Analysis & Solutions
// Harry He
public class Minimum {
public static int getMin(int numbers[]) {
int index1 = 0;
int index2 = numbers.length - 1;
int indexMid = index1;
while(numbers[index1] >= numbers[index2]) {
if(index2 - index1 == 1) {
indexMid = index2;
break;
}
indexMid = (index1 + index2) / 2;
// if numbers with indexes index1, index2, indexMid
// are equal, search sequentially
if(numbers[index1] == numbers[index2]
&& numbers[indexMid] == numbers[index1])
return getMinSequentially(numbers, index1, index2);
if(numbers[indexMid] >= numbers[index1])
index1 = indexMid;
else if(numbers[indexMid] <= numbers[index2])
index2 = indexMid;
}
return numbers[indexMid];
}
private static int getMinSequentially(int numbers[], int index1, int index2) {
int result = numbers[index1];
for(int i = index1 + 1; i <= index2; ++i) {
if(result > numbers[i])
result = numbers[i];
}
return result;
}
//================= Test Code =================
private static void test(String testName, int[] numbers, int expected) {
System.out.print(testName + " begins: ");
if(getMin(numbers) == expected)
System.out.print("passed.\n");
else
System.out.print("FAILED.\n");
}
public static void main(String argv[]) {
// typical input, a rotation of a increasingly sorted array
int array1[] = {3, 4, 5, 1, 2};
test("Test1", array1, 1);
// duplicated number is the minimal
int array2[] = {3, 4, 5, 1, 1, 2};
test("Test2", array2, 1);
// duplicated number is not in both beginning and end
int array3[] = {3, 4, 5, 1, 2, 2};
test("Test3", array3, 1);
// duplicated number is in both beginning and end
int array4[] = {1, 0, 1, 1, 1};
test("Test4", array4, 0);
// rotate 0 numbers in an array
int array5[] = {1, 2, 3, 4, 5};
test("Test5", array5, 1);
// only one number in an array
int array6[] = {2};
test("Test6", array6, 2);
}
}