标签:
同义词 二分查找法一般指二分查找
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
def binarySearch(lists,select): is_none = False iflists! = []: cen_num = len (lists) / 2 tlag = lists[cen_num] gt_list = lists[ 0 :cen_num] lt_list = lists[cen_num + 1 :] if tlag = = select: is_none = True return is_none elif tlag>select: is_se = binarySearch(gt_list,select) if notis_se: return binarySearch(lt_list,select) return is_none elif tlag<select: is_se = binarySearch(lt_list,select) if notis_se: return binarySearch(gt_list,select) return is_none |
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
|
programjjzx(input,output); var a: array [1..10]ofinteger; i,j,n,x:integer; begin [ ‘输入10个从小到大的数:‘ ] for i:=1 to 10 do read(a[i]); [ ‘输入要查找的数:‘ ] readln(x); i:=1;n:=10;j:=trunc((i+n)/2); repeat if a[j]>x then begin n:=j-1;j:=trunc((i+n)/2) end else if a[j]<x then begin i:=j+1;j:=trunc((i+n)/2) end ; until(a[j]=x) or (i>=n);{为什么是这个结束循环条件——i>n表示所查找的范围已经空了(就是没找到)} if a[j]=x then writeln( ‘查找成功!位置是:‘ ,j:3) else writeln( ‘查找失败,数组中无此元素!‘ ) end . |
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
|
int BinSearch(SeqList *R, int n,KeyType K) { //在有序表R[0..n-1]中进行二分查找,成功时返回结点的位置,失败时返回-1 int low=0,high=n-1,mid; //置当前查找区间上、下界的初值 while (low<=high) { if (R[low].key==K) return low; if (R[high].key==k) return high; //当前查找区间R[low..high]非空 mid=low+((high-low)/2); //使用(low+high)/2会有整数溢出的问题 //(问题会出现在当low+high的结果大于表达式结果类型所能表示的最大值时, //这样,产生溢出后再/2是不会产生正确结果的,而low+((high-low)/2)不存在这个问题 if (R[mid].key==K) return mid; //查找成功返回 if (R[mid].key<K) low=mid+1; //继续在R[mid+1..high]中查找 else high=mid-1; //继续在R[low..mid-1]中查找 } if (low>high) return -1; //当low>high时表示所查找区间内没有结果,查找失败 } //BinSeareh ------------上面代码复杂难懂----------------- int bsearchWithoutRecursion(intarray[], int low, int high, int target) { while (low<=high) { int mid=(low+high)/2; if (array[mid]>target) high=mid-1; elseif(array[mid]<target) low=mid+1; else //findthetarget return mid; } //the array does not contain the target return -1; } ---------------------------------------- |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
intbinary_search(constintarr[],intlow,inthigh,intkey) { int mid=low+(high-low)/2; if (low>high) return -1; else { if (arr[mid]==key) return mid; else if (arr[mid]>key) return binary_search(arr,low,mid-1,key); else return binary_search(arr,mid+1,high,key); } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
public static int binarySearch(Integer[] srcArray, int des) { int low = 0 ; int high = srcArray.length - 1 ; while ((low <= high) && (low <= srcArray.length - 1 ) && (high <= srcArray.length - 1 )) { int middle = (high + low) >> 1 ; if (des == srcArray[middle]) { return middle; } else if (des < srcArray[middle]) { high = middle - 1 ; } else { low = middle + 1 ; } } return - 1 ; } |
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
|
//二分查找 functionbinSearch(t,v){ varp=#t; varp2; varb=0; do { p2=p; p=b+math. floor ((p-b)/2) //二分折半运算 if (p==b) return ; if (t[p]<v){ //判断下限 b=p; p=p2; } } while (t[p]>v) //判断上限 returnt[p]==v&&p; } //测试 tab={} //创建数组,每个元素都是随机数 for (i=1;10;1){ tab[i]=math.random(1,10000) } //插入一个已知数 table.push(tab,5632) //排序 table.sort(tab) io.open() io.print( "数组" ,table.tostring(tab)) io.print( "使用二分查找,找到5632在位置:" ,binSearch(tab,5632)) } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
function binsearch($x,$a){ $c=count($a); $lower=0; $high=$c-1; while ($lower<=$high){ $middle=intval(($lower+$high)/2); if ($a[$middle]>$x) $high=$middle-1; elseif($a[$middle]<$x) $lower=$middle+1; else return $middle; } return -1; } |
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
|
int binSearch( const int *Array, int start, int end, int key) { int left,right; int mid; left=start; right=end; //注释中为递归算法,执行效率低,不推荐 /* if(key<Array[mid]) { return(binSearch(Array,left,mid,key)); } else if(key>Array[mid]) { return(binSearch(Array,mid+1,right,key)); } else return mid; */ while (left<=right) { mid=(left+right)/2; if (key==Array[mid]) return mid; else if (key<Array[mid]) right=mid-1; else if (key>Array[mid]) left=mid+1; } return -1; //找不到就返回-1 } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
publicstaticfunctionbinSearch(list:Array,low: int ,high: int ,key: int ): int { if (low>high) return -1; varmid: int =low+ int ((high-low)/2); varindex: int =-1 if (list[mid]==key){ index=mid; }elseif(list[mid]<key){ low=mid+1; index=binSearch(list,low,high,key); } else { high=mid-1; index=binSearch(list,low,high,key); }returnindex; } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
varArr = [ 3 , 5 , 6 , 7 , 9 , 12 , 15 ]; functionbinary(find,arr,low,high){ if (low< = high){ if (arr[low] = = find) returnlow; if (arr[high] = = find) returnhigh; varmid = Math.ceil((high + low) / 2 ); if (arr[mid] = = find){ returnmid; }elseif(arr[mid]>find){ returnbinary(find,arr,low,mid - 1 ); } else { returnbinary(find,arr,mid + 1 ,high); } } return - 1 ; } binary( 15 ,Arr, 0 ,Arr.length - 1 ); |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
/*二分查找:前提,该数组已经是一个有序数组,必须先排序,再查找。*/ functionbinarySearch(& $array , $findVal , $leftIndex , $rightIndex ){ $middleIndex = round (( $rightIndex + $leftIndex )/2); if ( $leftIndex > $rightIndex ){ echo ‘查无此数<br/>‘ ; return ; } if ( $findVal > $array [ $middleIndex ]){ binarySearch( $array , $findVal , $middleIndex +1, $rightIndex ); } elseif ( $findVal < $array [ $middleIndex ]){ binarySearch( $array , $findVal , $leftIndex , $middleIndex -1); } else { echo "找到数据:index=$middleIndex;value=$array[$middleIndex]<br/>" ; if ( $array [ $middleIndex +1]== $array [ $middleIndex ]&& $leftIndex < $rightIndex ){ binarySearch( $array , $findVal , $middleIndex +1, $rightIndex ); } if ( $array [ $middleIndex -1]== $array [ $middleIndex ]&& $leftIndex < $rightIndex ){ binarySearch( $array , $findVal , $leftIndex , $middleIndex -1); } } } |
标签:
原文地址:http://www.cnblogs.com/yg6405816/p/5479524.html