Binary Search with the find() Method
二分查找(java code)
1public int find(long searchkey)
2{
3 int lowerBound=0;
4 int upperBound=nelems-1;
5 int curIn;
6 while(true)
7 {
8 curIn=(lowerBound+upperBound)/2;
9 if(a[curIn]==searchKey)
10 return curIn;
11 else if(lowerBound>upperBound)
12 return nElems;
13 else
14 {
15 if(a[curIn]<searchKey]
16 lowerBound=curIn+1;
17 else
18 upperbound=curIn-1;
19 }
20 }
21}
2{
3 int lowerBound=0;
4 int upperBound=nelems-1;
5 int curIn;
6 while(true)
7 {
8 curIn=(lowerBound+upperBound)/2;
9 if(a[curIn]==searchKey)
10 return curIn;
11 else if(lowerBound>upperBound)
12 return nElems;
13 else
14 {
15 if(a[curIn]<searchKey]
16 lowerBound=curIn+1;
17 else
18 upperbound=curIn-1;
19 }
20 }
21}
The OrdArray Class (排序数组 java code)
1class OrdArray
2{
3 private long[] a;
4 private int nElems;
5
6 public OrdArray(int max)
7 {
8 a=new long[max];
9 nElems=0;
10 }
11
12 public int Size()
13 {
14 return nElems;
15 }
16
17 public int find(long searchKey)
18 {
19 int LowerBound=0;
20 int UpperBound=nElems-1;
21 int curIn;
22
23 while(true)
24 {
25 curIn=(LowerBound+UpperBound)/2;
26
27 if(a[curIn]==searchKey)
28
29 return curIn;
30 else if(LowerBound>UpperBound)
31
32 return nElems;
33
34 else
35 {
36 if(a[curIn]<searchKey)
37 {
38 LowerBound=curIn+1;
39 }
40 else
41 {
42 UpperBound=curIn-1;
43 }
44 }
45 }
46 }
47
48
49 public void Insert(long value)
50 {
51 int j;
52 for(j=0;j<nElems;j++)
53 if(a[j]>value)
54 break;
55
56 for(int k=nElems; k>j; k--)
57 {
58 a[k]=a[k-1];
59 }
60 a[j]=value;
61 nElems++;
62 }
63 }
64
65
66 public boolean delete(long value)
67 {
68 int j=find(value)
69
70 if(j==nElems)
71 return false;
72
73 else
74 {
75 for(int k=j;k<nElems;k++)
76 {
77 a[k]=a[k+1];
78 }
79 nElems--;
80
81 return true;
82 }
83 }
84
85 public void display()
86 {
87 for(int j=0;j<nElems;j++)
88 System.out.print(a[j]+"")
89 System.out.printIn("");
90 }
91}
92
93
2{
3 private long[] a;
4 private int nElems;
5
6 public OrdArray(int max)
7 {
8 a=new long[max];
9 nElems=0;
10 }
11
12 public int Size()
13 {
14 return nElems;
15 }
16
17 public int find(long searchKey)
18 {
19 int LowerBound=0;
20 int UpperBound=nElems-1;
21 int curIn;
22
23 while(true)
24 {
25 curIn=(LowerBound+UpperBound)/2;
26
27 if(a[curIn]==searchKey)
28
29 return curIn;
30 else if(LowerBound>UpperBound)
31
32 return nElems;
33
34 else
35 {
36 if(a[curIn]<searchKey)
37 {
38 LowerBound=curIn+1;
39 }
40 else
41 {
42 UpperBound=curIn-1;
43 }
44 }
45 }
46 }
47
48
49 public void Insert(long value)
50 {
51 int j;
52 for(j=0;j<nElems;j++)
53 if(a[j]>value)
54 break;
55
56 for(int k=nElems; k>j; k--)
57 {
58 a[k]=a[k-1];
59 }
60 a[j]=value;
61 nElems++;
62 }
63 }
64
65
66 public boolean delete(long value)
67 {
68 int j=find(value)
69
70 if(j==nElems)
71 return false;
72
73 else
74 {
75 for(int k=j;k<nElems;k++)
76 {
77 a[k]=a[k+1];
78 }
79 nElems--;
80
81 return true;
82 }
83 }
84
85 public void display()
86 {
87 for(int j=0;j<nElems;j++)
88 System.out.print(a[j]+"")
89 System.out.printIn("");
90 }
91}
92
93