Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Searching Algorithm (搜索专项讨论) #45

Open
YuezhenQin opened this issue Mar 17, 2024 · 2 comments
Open

Searching Algorithm (搜索专项讨论) #45

YuezhenQin opened this issue Mar 17, 2024 · 2 comments

Comments

@YuezhenQin
Copy link
Owner

YuezhenQin commented Mar 17, 2024

Searching Algorithm

LinearSearch

BinarySearch: searching for an element's index or insertion point if it doesn't exist.

SegmentTree

@YuezhenQin YuezhenQin converted this from a draft issue Mar 17, 2024
@YuezhenQin YuezhenQin changed the title 搜索算法总结 SearchingAlgorithm Mar 17, 2024
@YuezhenQin YuezhenQin changed the title SearchingAlgorithm Searching Algorithm Mar 17, 2024
@YuezhenQin YuezhenQin changed the title Searching Algorithm Searching Algorithm (搜索算法) Mar 17, 2024
@YuezhenQin
Copy link
Owner Author

YuezhenQin commented Mar 17, 2024

Binary Search

If you have a sorted array arr and an element x, then in O(logn) time and O(1) space, binary search can:

  1. find the index of x, if it is in arr
  2. find the first or the last index in which x can be inserted to maintain being sorted otherwise

Here's the idea behind binary search: We can discard the half that can't contain x, and then repeat the process on the other half. We continue this process of cutting the array in half until we find x.

public int binarySearch(int[] arr, int target) {
    /* define the current search space */
    int left = 0;
    int right = arr.length - 1;
    while (left <= right) {
        int mid = left + (right - left) / 2; /* halve the search space at every iteration */
        if (arr[mid] == target) return mid;
        if (arr[mid] > target) {
            right = mid - 1;
        } else {
            left = mid + 1;
        }
    }
    return left; /* left is at the insertion point */
}

In the Java and C++ implementations (with integer sum overflow), instead of doing (left + right) / 2, we do left + (right - left) / 2 to avoid overflow.

@YuezhenQin
Copy link
Owner Author

YuezhenQin commented Mar 19, 2024

If your input has duplicates, you can modify the binary search template to find either the first or the last position of a given element.

Find the left-most index Find the right-most index + 1 (insertion point)
Image Image

Regarding both of the above templates, if the element you are searching for does not exist, then left will be at the index where the element should be inserted to maintain sorted order

@YuezhenQin YuezhenQin changed the title Searching Algorithm (搜索算法) Searching Algorithm (搜索算法讨论) Jun 4, 2024
@YuezhenQin YuezhenQin changed the title Searching Algorithm (搜索算法讨论) Searching Algorithm (搜索专项讨论) Jun 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

No branches or pull requests

1 participant