二分查詢和遞迴的二分查詢

2021-07-13 17:36:18 字數 274 閱讀 1985

在乙個有序的陣列中查詢給定的資料項,把陣列衝中間分成兩半,然後看要查詢的資料項在陣列的哪一半,再次折半查詢。

**如下:

public int find(long searchkey)else if(lowerbound>upperbound)else

private int recfind(long searchkey,int lowerbound,int upperbound)else if(lowerbound>upperbound)else{

if(a[curin]

二分查詢及遞迴的二分查詢

一 思路 二分查詢又名折半查詢,顧名思義是分成兩部分,每查詢一次資料減半,所以查詢起來會很快。但是二分查詢的條件是已經有序的資料。1 設定兩個變數i,j.為查詢的範圍,i一般為0,j一般為陣列長度減一 2 設定變數mid,mid為所查陣列範圍的中間位置,即mid i j 2 3 比較所要查詢的資料v...

二分查詢與遞迴式二分查詢

二分查詢 public class binarysearch arrays.sort arr todo auto generated method stub int low 0 int high arr.length 1 while low high elseelse 遞迴實現二分查詢 public...

二分查詢(遞迴)

二分查詢 problem description 給出含有n個數的公升序序列,保證序列中的數兩兩不相等,這n個數編號從1 到n。然後給出q次詢問,每次詢問給出乙個數x,若x存在於此序列中,則輸出其編號,否則輸出 1。input 單組輸入。首先輸入乙個整數n 1 n n 3000000 接下的一行包含...