2012年11月17日
總想提筆寫點什么,卻不知道該怎樣記錄這幾個月的生活。滿腦的思緒卻無從下筆,只是感覺找工作也不是自己想的那么容易。看著一起找工作的人都找到了,就自己沒有找到合適的,就慢慢的開始懷疑自己,對生活和未來喪失信心。不過還好懂得,人最不能放棄的就是希望,好多事情盡了自己最大的努力就好。
1
package com.test2;
2
3
public class Demo2
{
4
5
/** *//**
6
* @param args
7
*/
8
9
public static int search(int[] arrays, int target)
{
10
11
int start = 0;
12
int end = arrays.length - 1;
13
int pos;
14
while (start <= end)
{
15
pos = (start + end) / 2;
16
if (arrays[pos] == target)
{
17
return pos;
18
} else if (arrays[pos] > target)
{// 如果數組中間的數大于目標,則將end的位置變成數組中間位置-1的位置
19
end = pos - 1;
20
} else
{
21
start = pos + 1;// 如果數組中間的數小于目標,則將start的位置變成數組中間位置+1的位置
22
}
23
}
24
return -1; // 若沒有查找到,則返回-1
25
}
26
27
public static void main(String[] args)
{
28
int[] arrays =
{ 2, 3, 28, 39, 59, 288, 322, 324, 2323 };
29
System.out.println(search(arrays, 28));
30
System.out.println(search(arrays, 322));
31
System.out.println(search(arrays, 59));
32
System.out.println(search(arrays, 288));
33
}
34
35
}
36