(Daily algorithm) LeetCode --- Search in Rotated Sorted Array (Binary Search for rotating arrays)

Source: Internet
Author: User

(Daily algorithm) LeetCode --- Search in Rotated Sorted Array (Binary Search for rotating arrays)
Search in Rotated Sorted Array I & II

Leetcode

Perform binary search for ordered arrays (the following uses a non-decreasing array as an example ):
 
 
  1. int binarySort(int A[], int lo, int hi, int target)
  2. {
  3. while(lo <= hi)
  4. {
  5. int mid = lo + (hi - lo)/2;
  6. if(A[mid] == target)
  7. return mid;
  8. if(A[mid] < target)
  9. lo = mid + 1;
  10. else
  11. hi = mid - 1;
  12. }
  13. }
Perform binary search for an ordered rotating array:

Eg. [7, 8, 9, 3, 4, 5, 6]

There is only one in the arrayBreakpoint(The number is increased or decreased ).

The key to achieving this is to cleverly bypass this breakpoint.

1. If the first half is ordered:

targetIn this rangehi = mid - 1

Not in this rangelo = mid + 1

2. If the first half is unordered

targetIn the ordered interval of the second halflo = mid + 1

Not in this rangehi = mid - 1

That is to say, we give priority to the ordered part.

 
 
  1. int search(int A[], int lo, int hi, int target)
  2. {
  3. while (lo <= hi)
  4. {
  5. int mid = (lo + hi) / 2;
  6. if (A[mid] == target)
  7. return mid;
  8. if (A[lo] <= A[mid])
  9. {
  10. if (A[lo] <= target && target < A[mid])
  11. hi = mid - 1;
  12. else
  13. lo = mid + 1;
  14. } else {
  15. if (A[mid] < target && target <= A[hi-1])
  16. lo = mid + 1;
  17. else
  18. hi = mid - 1;
  19. }
  20. }
  21. return -1;
  22. }
Perform binary search for the rotated array containing repeated elements:

If repeated elements are allowed, if A [m]> = A [l] in the previous question, the assumption that [l, m] is an incremental sequence cannot be true, for example, [1, 3, 1, 1].

If A [m]> = A [l] is not necessarily incremental, split it into two conditions:

If A [m]> A [l], the interval [l, m] increases progressively.

If A [m] = A [l] cannot be determined, then l ++. Let's take A look at it.

 
 
  1. bool search(int A[], int lo, int hi, int target)
  2. {
  3. while (lo <= hi)
  4. {
  5. int mid = (lo + hi) / 2;
  6. if (A[mid] == target)
  7. return true;
  8. if (A[lo] < A[mid])
  9. {
  10. if (A[lo] <= target && target < A[mid])
  11. hi = mid - 1;
  12. else
  13. lo = mid + 1;
  14. } else if(A[lo] > A[mid]) {
  15. if (A[mid] < target && target <= A[hi-1])
  16. lo = mid + 1;
  17. else
  18. hi = mid - 1;
  19. }
  20. else
  21. lo++;
  22. }
  23. return false;
  24. }

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.