[問題] 請問二元搜尋法的問題
請問二元搜尋法如何找重複數字呢?
例如: 1 3 3 5 5 7 9
-------------------------------
二元搜尋法只能搜尋一個數字嗎?
以及出現位置 都會不一定的嗎?
[補充]使用編譯器:Dev C++
編寫語言: C
※ 編輯: larrywhy 來自: 114.46.116.90 (10/10 22:34)
→
10/10 22:37, , 1F
10/10 22:37, 1F
int data::BinarySearch(int Input,int num)
{
int i,j;
int middle;
int left = 0;
int right = num-1;
while(left <= right)
{
middle = (left + right) / 2;
switch(compare(Arr[middle],Input))
{
case -1 : //if Arr [middle] < Input
left = middle + 1 ;
break;
case 0 : //if Arr [middle] == Input
return middle;
case 1 : //if Arr [middle] > Input
right = middle -1 ;
}
}
return -1;
}
------------------------------------------
以上是我的程式碼!!
請問如何修改才能往左右搜尋呢
因為他一找到就直接回傳了~
※ 編輯: larrywhy 來自: 114.46.116.90 (10/10 22:45)
推
10/10 23:00, , 2F
10/10 23:00, 2F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章