-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.cpp
More file actions
56 lines (54 loc) · 891 Bytes
/
Copy pathmain.cpp
File metadata and controls
56 lines (54 loc) · 891 Bytes
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
#include <iostream>
using namespace std;
int MinInOrder(int arr[],int pos1,int pos2)
{
int min =pos1;
int i=0;
for (i=pos1;i<=pos2;i++)
{
if (arr[i]<arr[min])
{
min=i;
}
}
return arr[min];
}
int Min(int arr[],int len)
{
int mid=0;
int pos1 =0;
int pos2 =len-1;
while (arr[pos1]>=arr[pos2])
{
if(pos2-pos1 ==1)
{
mid=pos2;
break;
}
mid = (pos2+pos1)/2;
if (arr[pos1]==arr[pos2] && arr[mid]==arr[pos1])
{
return MinInOrder(arr,pos1,pos2);
}
if (arr[mid]<= arr[pos1])
{
pos2=mid;
}
else if(arr[mid]>=arr[pos1])
{
pos1=mid;
}
}
return arr[mid];
}
int main()
{
//int arr[10]={3,4,5,6,7,8,9,0,1,2};
//int arr[]={1,1,0,1,1,1,1};
int arr[]={1,1,1,0,1};
int len=sizeof(arr)/sizeof(arr[0]);
int min=Min(arr,len);
cout<<"min num is:"<<min<<endl;
system("pause");
return 0;
}