Create 01-复杂度3 二分查找 (20 分).c

This commit is contained in:
hao14293
2019-05-01 13:34:19 +08:00
committed by GitHub
parent 8883d67e71
commit 44382c78ce

View File

@@ -0,0 +1,21 @@
Position BinarySearch( List L, ElementType X ){
int p = 0;
int s = 1;
int e = L->Last;
int num = e/2;
while(num--){
int mid = (s + e) / 2;
if(L->Data[mid] == X){
p = mid;
break;
}else if(L->Data[mid] > X){
e = mid - 1;
}else if(L->Data[mid] < X){
s = mid + 1;
}
}
if(p == 0)
return NotFound;
return p;
}