Regular binary search

 1 class Solution {
 2 public:
 3     int searchInsert(int A[], int n, int target) {
 4         int start = 0, end = n-1, mid = 0;
 5         while (start <= end) {
 6             mid = (start + end)/2;
 7             if (A[mid] >= target) end = mid-1;
 8             else start = mid + 1;
 9         }
10         return start;
11     }
12 };

 

posted on 2015-03-23 13:23  keepshuatishuati  阅读(87)  评论(0编辑  收藏  举报