Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- int BinSearch(const int* arr, int amount) {
- int mid = 0, end = 0;
- int begin = 0, mid_index = 0;
- end = amount - 1;
- while (end - begin >= 1) {
- if (arr[begin] < arr[end]) {
- return begin;
- }
- mid = arr[(begin + end) / 2];
- mid_index = (begin + end) / 2;
- if (mid_index == 0) {
- return end;
- }
- if (mid < arr[mid_index - 1] && mid < arr[mid_index + 1]) {
- return mid_index;
- }
- if (mid > arr[mid_index - 1] && mid > arr[mid_index + 1]) {
- return mid_index + 1;
- }
- if (mid < arr[end] && mid_index > 0) {
- end = mid_index - 1;
- } else if (mid > arr[begin]) {
- begin = mid_index + 1;
- } else {
- return 0;
- }
- }
- return 0;
- }
- void FindShift() {
- int amount = 0;
- std::cin >> amount;
- int* arr = new int[amount];
- for (int i = 0; i < amount; i++) {
- std::cin >> arr[i];
- }
- std::cout << BinSearch(arr, amount);
- delete[] arr;
- }
- int main() {
- FindShift();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement