Algorithms

BinarySearch

int binarySearch(const vector& a, const int target) {
    int l = 0, r = a.size()-1;
    while (l <= r) {
        int mid = l + (r - l) / 2;
        if (a[mid] == target) {
            return mid;
        } else if (a[mid] > target) {
            r = mid - 1;
        } else {
            l = mid + 1;
        }
    }
    return -1;
}

Sort

  1. Selection sort
  2. Insertion sort

你可能感兴趣的:(Algorithms)