Binary Search Algorithm

二分查找代码:

//============================================================================
// Name        : BinarySearch.cpp
// Author      : Danny
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
using namespace std;

int binarySearch(int a[], int left, int right, int k) {
    if (left > right)
        return -1;
    int mid = (left + right) / 2;
    if (a[mid] == k) {
        return mid;
    } else if (a[mid] > k) {
        return binarySearch(a, left, mid - 1, k);
    } else {
        return binarySearch(a, mid + 1, right, k);
    }
}

int main() {
    int a[5] = { 1, 2, 4, 5, 7 };
    int index = binarySearch(a, 0, 4, 7);
    cout << index << endl;
    return 0;
}
时间: 2024-10-24 01:42:49

Binary Search Algorithm的相关文章

[Algorithms] Binary Search Algorithm using TypeScript

(binary search trees) which form the basis of modern databases and immutable data structures. Binary search works very much the way humans intuitively search for a name in a yellow pages directory (if you have ever seen one) or the dictionary. In thi

[Math] Beating the binary search algorithm – interpolation search, galloping search

From: http://blog.jobbole.com/73517/ 二分检索是查找有序数组最简单然而最有效的算法之一.现在的问题是,更复杂的算法能不能做的更好?我们先看一下其他方法. 有些情况下,散列整个数据集是不可行的,或者要求既查找位置,又查找数据本身.这个时候,用哈希表就不能实现O(1)的运行时间了.但对有序数组, 采用分治法通常可以实现O(log(n))的最坏运行时间. 在下结论前,有一点值得注意,那就是可以从很多方面“击败”一个算法:所需的空间,所需的运行时间,对底层数据结构的访

Binary Search - Jump on the Stones

Binary Search algorithm. Wikipedia definition: In computer science, binary search, also known as half-interval search, logarithmic search, or binary chop, is a search algorithm that finds the position of a target value within a sorted array. Binary s

[Algorithm] Delete a node from Binary Search Tree

The solution for the problem can be divided into three cases: case 1: if the delete node is leaf node, then we can simply remove it case 2: if the delete node is has single side or substree case 3: it has two children, then to keep it as a valid bina

2 - Binary Search &amp; LogN Algorithm

254. Drop Eggs https://www.lintcode.com/problem/drop-eggs/description?_from=ladder&&fromId=1 28. Search a 2D Matrix https://www.lintcode.com/problem/search-a-2d-matrix/description?_from=ladder&&fromId=1 思路1: 1. find the row index, the last

ZOJ - 2243 - Binary Search Heap Construction

先上题目: Binary Search Heap Construction Time Limit: 5 Seconds      Memory Limit: 32768 KB Read the statement of problem G for the definitions concerning trees. In the following we define the basic terminology of heaps. A heap is a tree whose internal n

[leetcode-95-Unique Binary Search Trees II]

Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example,Given n = 3, your program should return all 5 unique BST's shown below. 1 3 3 2 1 \ / / / \ 3 2 1 1 3 2 / / \ 2 1 2 3 思路: This probl

4.7 Lowest Common Ancestor of a Binary Search Tree 二叉树的最小共同父节点

4.7 Design an algorithm and write code to find the first common ancestor of two nodes in a binary tree. Avoid storing additional nodes in a data structure. NOTE: This is not necessarily a binary search tree. LeetCode上的原题,请参见我之前的博客Lowest Common Ancest

12. binary search Trees

12. binary search Trees? ? The search tree data structure supports many dynamic-set operations,including search ,minimum,maximum,predecessor,successor ,insert ,and delete.? Thus, we can use a search tree both as a dictionary and as a priority queue.