diff --git "a/18\350\275\257\344\273\266\345\267\245\347\250\2131\347\217\255180021104224\346\236\227\344\277\212\351\224\213.txt.cpp" "b/18\350\275\257\344\273\266\345\267\245\347\250\2131\347\217\255180021104224\346\236\227\344\277\212\351\224\213.txt.cpp" new file mode 100644 index 0000000..6362110 --- /dev/null +++ "b/18\350\275\257\344\273\266\345\267\245\347\250\2131\347\217\255180021104224\346\236\227\344\277\212\351\224\213.txt.cpp" @@ -0,0 +1,53 @@ + + #include + #include + using namespace std; + + template + int BinarySearch(Type a[],const Type& x,int n); + + int main() + { + int x = 10; + int a[20]; + for(int i=0; i<20; i++) + { + a[i] = i + 1; + } + for(int i=0; i<20; i++) + { + cout< + int BinarySearch(Type a[],const Type& x,int n) + { + int left = 0; + int right = n-1; + while(left<=right) + { + int mid = (left + right)/2; + if(x == a[mid]) + { + return mid; + } + if(x>a[mid]) + { + left = mid + 1; + } + else + { + right = mid - 1; + } + } + + return -1; + } +