Code Skiller logoCB Logo
Logo LearnLearnLogo PracticePracticeLogo HireHireLogo IDEIDE

Binary Search

User image

Published by

sanya sanya

Published at: 1st Aug, 2023
1.96 mins read

A Brief Introduction to Binary Search

Binary search is used for searching in sorted arrays or lists only. It follows a divide-and-conquer approach by repeatedly dividing the search space in half until the target element is found or it is determined that the element does not exist in the array.

Duplicate Elements

When performing a binary search in an array with duplicate elements, you might want to find the leftmost occurrence of a specific element. When performing binary search on an array with duplicate elements, the standard binary search algorithm may not return the rightmost occurrence of the target element.

An analysis of Binary Search based on its Performance

In binary search, the algorithm only needs a few variables to keep track of the indices and values during the search process. These variables typically include the low and high indices representing the boundaries of the search space, the middle index for calculating the midpoint, and possibly a variable to store the result or target element.

Binary search versus other schemes

Binary search vs trees

  1. Structure:
  • Binary Search: Binary search is a search algorithm that operates on a sorted array or list. It uses a divide-and-conquer approach by repeatedly dividing the search space in half, narrowing down the possibilities until the target element is found or determined to be absent. Binary search does not involve a separate data structure; it performs the search directly on the given array or list.

  • Trees: Trees, such as binary search trees (BSTs), are hierarchical data structures. They consist of nodes connected by edges, with each node having zero or more child nodes. In a binary search tree, the nodes are organized such that the value of each node is greater than all values in its left subtree and less than all values in its right subtree. This property enables efficient searching, insertion, and deletion operations in logarithmic time.

What are some interesting variations of Binary Search?

Uniform binary search, also known as interpolation search, is an enhancement of the traditional binary search algorithm. While binary search divides the search space in half at each step, uniform binary search uses interpolation to estimate the position of the target element within the search space. This estimation allows for a more intelligent selection of the next element to be compared, potentially reducing the number of comparisons required.

Library

WEB DEVELOPMENT

FAANG QUESTIONS