利用遞歸寫二分查找法

public static int rank(int key,int[] a){
  return rank(key,a,0,a.length-1);
}

public static int rank(int key,int[] a,int lo,int hi)
{
  if(lo>hi) return -1;
  int mid = lo + (hi-lo)/2;
  if(key < a[mid]) return rank(key,a,lo,mid-1);
  else if(key > a[mid]) return rank(key,a,mid+1,hi);
  else return mid;
}

 

發佈了38 篇原創文章 · 獲贊 5 · 訪問量 6782
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章