Summary: Binary Search

Iterative ways:

 1 int binarySearch (int[] a, int x) {
 2    int low = 0;
 3    int high = a.length - 1;
 4    int mid;
 5
 6    while (low <= high) {
 7       mid = (low + high) / 2;
 8       if (a[mid] < x) {
 9          low = mid + 1;
10       }
11       else if (a[mid] > x) {
12          high = mid - 1;
13       }
14       else {
15          return mid;
16       }
17    }
18    return -1;
19 }

Recursive ways:

 1 int binarySearchRecursive (int[] a, int x, int low, int high) {
 2     if (low > high) return -1; //error
 3
 4     int mid = (low + high) / 2;
 5     if (a[mid] < x) {
 6         return binarySearchRecursive(a, x, mid + 1, high);
 7     }
 8     else if (a[mid] > x) {
 9         return binarySearchRecursive(a, x, low, mid - 1);
10     }
11     else {
12         return mid;
13     }
14 }

Summary: Binary Search,布布扣,bubuko.com

时间: 2024-10-16 00:29:27

Summary: Binary Search的相关文章

Binary search tree system and method

A binary search tree is provided for efficiently organizing values for a set of items, even when values are duplicated. In generating the binary search tree, the value of each item in a set of values is determined. If a particular value is unique and

Method for balancing binary search trees

Method for balancing a?binary?search?tree. A computer implemented method for balancing a?binary?search?tree includes locating a node in a?binary?search?tree, determining whether a depth of the located node is greater than a threshold, and performing

04-树6 Complete Binary Search Tree (30 分)

04-树6 Complete Binary Search Tree (30 分) A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a

[LeetCode] Find Mode in Binary Search Tree 找二分搜索数的众数

Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred element) in the given BST. Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than or equal to the nod

235. Lowest Common Ancestor of a Binary Search Tree

1. 问题描述 Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes v and w as the lowest node in T th

leetcode 109 Convert Sorted List to Binary Search Tree

题目连接 https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/ Convert Sorted List to Binary Search Tree Description Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. /** * De

Lowest Common Ancestor of a Binary Search Tree

1. Title 235. Lowest Common Ancestor of a Binary Search Tree 2. Http address https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/ 3. The question Given a binary search tree (BST), find the lowest common ancestor (LCA) of two

LeetCode 50 Pow(x, n)(Math、Binary Search)(*)

翻译 实现pow(x, n). 原文 Implement pow(x, n). 分析 首先给大家推荐维基百科: zh.wikipedia.org/wiki/二元搜尋樹 en.wikipedia.org/wiki/Binary_search_tree 其次,大家也可以看看类似的一道题: LeetCode 69 Sqrt(x)(Math.Binary Search)(*) 然而这题我还是没有解出来,看看别人的解法-- class Solution { private: double myPowHel

LeetCode OJ :Unique Binary Search Trees II(唯一二叉搜索树)

题目如下所示:返回的结果是一个Node的Vector: Given 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