二分搜索(Binary Search)

当我们在字典中查找某个单的时候,一般我们会翻到一个大致的位置(假设吧,翻到中间位置),开始查找。如果翻到的正好有我们要的词,那运气好,查找结束。如果我们要找的词还在这个位置的前面,那我们对前面的这一半词典继续搜索,翻到某个位置(假设是四分之一的位置)等等。这个二分搜索的工作原理一样。相应的算法就叫做二进制搜索算法。

迭代版本算法:

//iterative binary search which returns index of element
int binarySearchIterative(int arr[], int size, int data)
{
    int low = 0;
    int high = size-1;
    int mid;

    while(low<=high)
    {
        mid = low + (high-low)/2; //To avoid overflow by (low + high)

        if(arr[mid] == data)
            return mid;
        else
        {
            if(arr[mid] < data)
                low = mid + 1; // search in right half
            else
                high = mid - 1; // search in left half
        }
    }

    return -1;
}

递归版本算法:

//recursive binary search which returns index of element
int binarySearchRecursive(int arr[], int low, int high, int data)
{
    if(low<=high)
    {
        int mid = low + (high-low)/2; // To avoid overflow

        if(arr[mid] == data)
            return mid;
        else
        {
            if(arr[mid] < data)
                //search in right half.
                return binarySearchRecursive(arr, mid+1, high, data);
            else
                //search in left half.
                return binarySearchRecursive(arr, low, mid-1, data);
        }
    }
    return -1;
}
时间: 2024-10-24 16:26:55

二分搜索(Binary Search)的相关文章

二分搜索 - Binary Search

二分搜索是一种在有序数组中寻找目标值的经典方法,也就是说使用前提是『有序数组』.非常简单的题中『有序』特征非常明显,但更多时候可能需要我们自己去构造『有序数组』.下面我们从最基本的二分搜索开始逐步深入. 一.lower/upper bound 定义 lower bound 为在给定升序数组中大于等于目标值的最小索引,upper bound 则为小于等于目标值的最大索引,下面上代码和测试用例. import java.util.*; public class Main { public stati

《算法导论》习题2.3-5 二分搜索 Binary Search

地球人都知道“二分查找”,方法也非常简单,但是你能不能在10分钟内写出一个没有bug的程序呢? 知易行难,自己动手写一下试一试吧. public class BinarySearch { public static int search(int [] A,int target,int a, int b) { int middle = (a+b)/2; if(a>b) return -1; else if (A[middle]==target) return middle; else if (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

Binary Search and Quicksort

1. stdlib/bsearch.c 以下代码实现了有序数组的二分搜索. 1 /* $NetBSD: bsearch.c,v 1.14.6.1 2012/04/17 00:05:25 yamt Exp $ */ 2 3 /* 4 * Copyright (c) 1990, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in sou

72【leetcode】经典算法- Lowest Common Ancestor of a Binary Search Tree(lct of bst)

题目描述: 一个二叉搜索树,给定两个节点a,b,求最小的公共祖先 _______6______ / ___2__ ___8__ / \ / 0 _4 7 9 / 3 5 例如: 2,8 -->6 2,4-–>2 原文描述: 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

(双指针、二分Binary Search) leetcode 658. Find K closest Elements

题意:给定一个升序排列的数组,找到k个与x最相近的元素(即差值最小),返回的结果必须要是按升序排好的.如果有两个数与 x的差值一样,优先选择数值较小的那个数. 解法一:双指针(排除法),一个一个删,因为是有序数组,且返回的是连续升序子数组,所以每一次删除的元素一定是位于边界:如果数组含有共 7 个元素,要保留 3 个元素,因此要删除 4 个元素(arr.size()-k):因为要删除的元素都位于边界,于是可以使用双指针(左指针指向数组的第一个元素,右指针指向数组最后一个元素)对撞的方式确定保留区

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