-
Notifications
You must be signed in to change notification settings - Fork 0
/
find_minimum2.c
97 lines (83 loc) · 1.82 KB
/
find_minimum2.c
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
* =====================================================================================
*
* Filename: find_minimum2.c
*
* Description: Find Minimum in Rotated Sorted Array II.
* The array may contain duplicates.
*
* Version: 1.0
* Created: 2015/02/25 22时55分57秒
* Revision: none
* Compiler: gcc
*
* Author: Zhu Xianfeng <[email protected]>
* Organization:
*
* =====================================================================================
*/
#include <stdio.h>
#include <stdlib.h>
static int
_findMin(int arr[], int i, int j)
{
int m;
if (i == j) {
return i;
} else if (i == (j - 1)) {
if (arr[i] < arr[j]) {
return i;
} else {
return j;
}
}
m = (i + j) / 2;
if (arr[i] > arr[m]) {
return _findMin(arr, i, m);
} else if (arr[m] > arr[j]) {
return _findMin(arr, m, j);
} else if (arr[i] == arr[m] || arr[m] == arr[j]) {
int lmin;
int rmin;
if (arr[i] == arr[m] && arr[m] == arr[j]) {
lmin = _findMin(arr, i, m - 1);
rmin = _findMin(arr, m + 1, j);
} else if (arr[i] == arr[m]) {
lmin = _findMin(arr, i, m - 1);
int rmin = _findMin(arr, m, j);
} else {
/* arr[m] == arr[j] */
lmin = _findMin(arr, i, m);
rmin = _findMin(arr, m + 1, j);
}
if (arr[lmin] < arr[rmin]) {
return lmin;
} else {
return rmin;
}
} else {
/* Normal sorted list */
return i;
}
}
int
findMin(int num[], int n)
{
int idx;
idx = _findMin(num, 0, n - 1);
return num[idx];
}
int main(int argc, char *argv[])
{
int arr1[] = {4, 5, 6, 7, 0, 1, 2};
int arr2[] = {1, 2, 3, 4, 5, 6, 7};
int arr3[] = {3, 3, 1, 3};
int min;
min = findMin(arr1, 7);
printf("Min: %d\n", min);
min = findMin(arr2, 7);
printf("Min: %d\n", min);
min = findMin(arr3, 4);
printf("Min: %d\n", min);
return 0;
}