Setup Post
  • Home
  • Business
  • Computers and Technology
  • Health
  • Services
  • write for us
  • contact
Friday, March 24, 2023
No Result
View All Result
  • Home
  • Business
  • Computers and Technology
  • Health
  • Services
  • write for us
  • contact
No Result
View All Result
Setup Post
No Result
View All Result
Home Computers and Technology

Beginner’s Guide To Binary Search Algorithm

Lindsay by Lindsay
December 14, 2021
in Computers and Technology
0
what is binary search
Share on FacebookShare on Twitter

Binary search is one of the most popular methods of locating a value in computer science. When it comes to a sorted array in a large database, it works perfectly. Usually, it is one of the most efficient search techniques. In today’s share from algo.monster, we will give a brief analysis of this technique. Hopefully, you will get a better understanding of it after finishing our article today.

Contents hide
1 A Short Introduction to Binary Search
2 How does this search method work?
3 Algorithm Binary Search
4 A sample of this search technique
5 Binary search time complexity
6 Time Analysis (recursive, iterative)
6.1 Time Analysis for the Best Case
6.2 Time Analysis for the Worst Case
6.3 Time Analysis for the Average Case
7 Advantages and Disadvantages of the binary search algorithm
8 Binary Search Application: finding the word you want in a dictionary
9 Conclusion

A Short Introduction to Binary Search

Let’s start by learning the basics of this computer algorithm.

People often call binary search half-interval or logarithmic search. It is a search method that people use to locate the target value in a sorted array.

Binary search runs at the worst logarithmic speed, making O (log n) comparisons. where n represents the number of elements in an array, and the log is binary logarithm and uses only constant O(1)space.

How does this search method work?

This involves finding the element in the middle and comparing its key value to the element in that element. If the value is lower than the element at the middle, then you will need to search for the key to the list from the middle element to search for the other key.

Algorithm Binary Search

{( A[0 1 2 3…….n-1] , key) low 0 high n-1 while(low <= high) do (A[0 1 2 3 …….n-1] key) low high n-1 while (low = high), do m(low + high),/2 if (key=A[m]).

Algorithm return m else if (key<A[m]) then highß m-1 else lowß m+1 }

A sample of this search technique

Let 4,6,7,9 and 10, respectively, by the numbers given. The key is 9.

According to the algorithm, these numbers should be stored in a 1D array called A[0 2 n-1] where n= number elements. In this example, 5.

According to the algorithm, A[0 2 3 4] is created and the key value is passed into the array that is 9. Binary Search ( A[0 2 3 4] – 9 ) low=0 high=4 while (0=4)

Example Iteration 1 – m=2 low=3 //here Key=9 is greater than A[m]=7, so it executes the last other condition. Then, the loop of (34) will continue.

Iteration 2 – m=3 return 3.

Binary search time complexity

Let’s say we have an array of A. We are looking for K.

It takes, on average and in the worst cases, linear time, or O(N) to count the array items.

If A is a sorted array, however, Binary Search is faster to find K. Binary search cuts the length of the array after each iteration.

Time Analysis (recursive, iterative)

Binary Search can be analyzed with the average, worst, or best case number of comparisons. In fact, you do this analysis based on the length of the arrays. Suppose N=|A| is the length of Array A.

If comparison counting is slightly relaxed, the numbers of logarithmic search comparisons are equal.

Recursive binary search counts each pass through the If-then-else block as one comparison.

On the other hand, iterative logarithmic search counts each passes through the while blocks as one comparison.

Time Analysis for the Best Case

Best case – O (1) comparisons: In the ideal case, item X is located in the middle of array A. You will need to make a constant number of comparisons (actually, only 1)

Time Analysis for the Worst Case

Worst Case – O (log N) comparisons: In the worst scenario, item X is not found in array A. Binary Search reduces the permissible range by halving the size with each iteration or recursion. Ceiling (log n) can be used to halve the range. Ceiling (log n) comparisons are therefore required.

Time Analysis for the Average Case

Average case – O(log n) comparisons: To calculate the average case, multiply the number of comparisons needed to find each element by the probability of finding it. For simplicity, let’s assume that every item in A will be searched and that all probabilities of finding each element are equal.

Advantages and Disadvantages of the binary search algorithm

Advantage: Binary search is an efficient searching algorithm that allows us to find the desired element quickly. In fact, it is one of the quickest when it comes to sorted arrays within a larger database.

Disadvantage: One of the most annoying things about this method is that it is not suitable for unsorted arrays. In other words, you need to sort the elements in order before you can use this method. Actually, this is the only way to apply this search algorithm.

Binary Search Application: finding the word you want in a dictionary

Binary search is a powerful search tool that enables people to find desired records from a database. It often increases efficiency and saves time.

Below is a common application of this search algorithm in real life.

what is binary search

Surprisingly, but you can use it to easily spot any word you want to find. As we know, there are thousands of words in a dictionary. If you want to find some words, it takes a while. But with the help of a binary search algorithm, it’s a piece of cake.

For example, if you want to find the word “Voracious”. You can compare the middle page with the desired word first. If this word is alphabetically lower than the word on the middle page, we can stop searching from the higher part. That means, we can save half-time by only looking for it from the remaining left half. On the other hand, if it is bigger than the middle page word, similarly, we ignore the left half.

And the process won’t stop until you finally find the word you want.

Conclusion 

If you carefully think about the example above, it’s easy to find out that the logarithmic search algorithm actually eliminates half of the elements each time. Thus, locating a single value from numerous numbers or a huge database saves time. Obviously, it saves much time.

For further information about search methods or other algorithms, you can check algo.monster out. They offer useful courses and lectures for beginners. Well, if you’re an expert already, you can still find something wonderful there. Enjoy your searching with a binary search algorithm.

 

Tags: binary searchsearch algorithm
Previous Post

Body lotion application

Next Post

How to Save Attachments from EML Files?

Related Posts

Business

AWS: Multi-Tenancy Creates Confused Deputies

January 3, 2023
benefits of gaming
Gaming

Benefits of gaming you never knew about

October 17, 2022
Computers and Technology

ESPN Activate

October 16, 2022
Computers and Technology

Affordable MacBook repair Dubai.

October 15, 2022
battery management system
Computers and Technology

How does ford battery management system work?

October 14, 2022
Computers and Technology

Simplifying the Science of Storage with SAN Storage Solutions

October 14, 2022
Next Post
save attachments from eml files

How to Save Attachments from EML Files?

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Global Bioprocess Technology Market Key Player, Growth, with Covid-19 Impact Analysis, Share, Size, Leading Players, Industry Growth And Forecast 2027

March 16, 2022

Why You Should Visit Rythmia Life Advancement Center

August 20, 2022
Wild Magic

Funniest Wild Magic instance for your sorcerer? 

June 22, 2022
engineering colleges in Haryana

Study at the Top Artificial Intelligence Colleges in Jaipur

July 16, 2022

What Is the Best Way to Sell My Car Quickly?

April 25, 2022
Email Marketing

How to Make Your Email Marketing Campaign Stand Out From the Crowd

January 25, 2022
Charlotte Perriand furniture

Iconic Chairs & Stools For Your Home

April 26, 2022

Why are  ortholite fusion insoles gaining a lot of popularity for production of shoes?

April 11, 2022

Buy 4000 Watch Hours For YouTube For Monetization

February 28, 2022

The Most Recent Trends in YouTube Music Video Promotion Service

June 18, 2022

Resolver Markt Konzentrieren Sie sich auf potenzielle Chancen für Schlüsselunternehmen – Moog, Danaher Corporation (including Hengstler), TAMAGAWA SEIKI CO.,LTD., LTN Servotechnik GmbH

May 26, 2022

3 Common Mistakes During Office Relocation

July 12, 2022

Business Jet Market is expected to grow at a CAGR of around 5% during 2021-2026

February 8, 2022

Aluminium-Silizium-Legierung Markt Unternehmen sind führend – CPS , ASM International, Osprey , TTC 

May 26, 2022

Modern Farming / New Agricultural Technologies in India

February 22, 2022

Pectin Manufacturing Project Report and Plant Cost 2022: Manufacturing Process, Raw Materials, Business Plan, Industry Trends, Machinery Requirements 2027 – Syndicated Analytics

April 14, 2022

© 2021 Setup Post all rights reserved.

No Result
View All Result
  • Home
  • Entertainment
    • Gaming
    • Movie
    • Music
    • Sports
  • Lifestyle
    • Fashion
    • Food
    • Travel
    • Health
  • News
    • Bussiness
    • Politics
    • Science
    • World
  • Tech
    • Apps
    • Gadget
    • Mobile

© 2021 Setup Post all rights reserved.