60 lines
1.7 KiB
C++
60 lines
1.7 KiB
C++
|
/**
|
|||
|
* File: binary_search.cpp
|
|||
|
* Created Time: 2022-11-25
|
|||
|
* Author: krahets (krahets@163.com)
|
|||
|
*/
|
|||
|
|
|||
|
#include "../utils/common.hpp"
|
|||
|
|
|||
|
/* <20><><EFBFBD>ֲ<EFBFBD><D6B2>ң<EFBFBD>˫<EFBFBD><CBAB><EFBFBD><EFBFBD><EFBFBD>䣩 */
|
|||
|
int binarySearch(vector<int> &nums, int target) {
|
|||
|
// <20><>ʼ<EFBFBD><CABC>˫<EFBFBD><CBAB><EFBFBD><EFBFBD><EFBFBD><EFBFBD> [0, n-1] <20><><EFBFBD><EFBFBD> i, j <20>ֱ<EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD>ء<EFBFBD>βԪ<CEB2><D4AA>
|
|||
|
int i = 0, j = nums.size() - 1;
|
|||
|
// ѭ<><D1AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> i > j ʱΪ<CAB1>գ<EFBFBD>
|
|||
|
while (i <= j) {
|
|||
|
int m = i + (j - i) / 2; // <20><><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD> m
|
|||
|
if (nums[m] < target) // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD><CBB5> target <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> [m+1, j] <20><>
|
|||
|
i = m + 1;
|
|||
|
else if (nums[m] > target) // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD><CBB5> target <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> [i, m-1] <20><>
|
|||
|
j = m - 1;
|
|||
|
else // <20>ҵ<EFBFBD>Ŀ<EFBFBD><C4BF>Ԫ<EFBFBD>أ<EFBFBD><D8A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
return m;
|
|||
|
}
|
|||
|
// δ<>ҵ<EFBFBD>Ŀ<EFBFBD><C4BF>Ԫ<EFBFBD>أ<EFBFBD><D8A3><EFBFBD><EFBFBD><EFBFBD> -1
|
|||
|
return -1;
|
|||
|
}
|
|||
|
|
|||
|
/* <20><><EFBFBD>ֲ<EFBFBD><D6B2>ң<EFBFBD><D2A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ҿ<EFBFBD><D2BF><EFBFBD><EFBFBD>䣩 */
|
|||
|
int binarySearchLCRO(vector<int> &nums, int target) {
|
|||
|
// <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ҿ<EFBFBD><D2BF><EFBFBD><EFBFBD><EFBFBD> [0, n) <20><><EFBFBD><EFBFBD> i, j <20>ֱ<EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD>ء<EFBFBD>βԪ<CEB2><D4AA>+1
|
|||
|
int i = 0, j = nums.size();
|
|||
|
// ѭ<><D1AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> i = j ʱΪ<CAB1>գ<EFBFBD>
|
|||
|
while (i < j) {
|
|||
|
int m = i + (j - i) / 2; // <20><><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD> m
|
|||
|
if (nums[m] < target) // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD><CBB5> target <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> [m+1, j) <20><>
|
|||
|
i = m + 1;
|
|||
|
else if (nums[m] > target) // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD><CBB5> target <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> [i, m) <20><>
|
|||
|
j = m;
|
|||
|
else // <20>ҵ<EFBFBD>Ŀ<EFBFBD><C4BF>Ԫ<EFBFBD>أ<EFBFBD><D8A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
return m;
|
|||
|
}
|
|||
|
// δ<>ҵ<EFBFBD>Ŀ<EFBFBD><C4BF>Ԫ<EFBFBD>أ<EFBFBD><D8A3><EFBFBD><EFBFBD><EFBFBD> -1
|
|||
|
return -1;
|
|||
|
}
|
|||
|
|
|||
|
/* Driver Code */
|
|||
|
int main() {
|
|||
|
int target = 6;
|
|||
|
vector<int> nums = {1, 3, 6, 8, 12, 15, 23, 26, 31, 35};
|
|||
|
|
|||
|
/* <20><><EFBFBD>ֲ<EFBFBD><D6B2>ң<EFBFBD>˫<EFBFBD><CBAB><EFBFBD><EFBFBD><EFBFBD>䣩 */
|
|||
|
int index = binarySearch(nums, target);
|
|||
|
cout << "Ŀ<EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD> 6 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> = " << index << endl;
|
|||
|
|
|||
|
/* <20><><EFBFBD>ֲ<EFBFBD><D6B2>ң<EFBFBD><D2A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ҿ<EFBFBD><D2BF><EFBFBD><EFBFBD>䣩 */
|
|||
|
index = binarySearchLCRO(nums, target);
|
|||
|
cout << "Ŀ<EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD> 6 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> = " << index << endl;
|
|||
|
|
|||
|
return 0;
|
|||
|
}
|