You are reading the article Binary Search In Data Structure updated in October 2023 on the website Phuhoabeautyspa.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested November 2023 Binary Search In Data StructureDefinition of Binary Search in Data Structure
Hadoop, Data Science, Statistics & othersSyntax
BINARY_SEARCH (LIST, LEFT_VALUE, RIGHT_VALUE, VALUE)
Step 1: First we need to initialize the variable such as SET START = LEFT_VALUE
Step 2: Repeat steps 3 and 4 until START is less than or equal to END i.e START<=END.
Step 3: In step 3 we need to find the mid-value by using the following formula.
SET MID_PT = (START + END)/2
Step 4: IF LIST [MID_PT] = VALUE
[HERE END IF LOOP]
Step 5: IF LOC = -1
Then print values not present in list or array.
End of IF loop
Step 6: Exit.
Explanation: In the above syntax that we also called the algorithm, here we use different steps to implement the binary search. In the first step, we need to initialize the variable such as LIST, LEFT_VALUE, RIGHT_VALUE and VALUE. In the second step, we compare two parts of the list that mean the left part is less than or equal to the right part then we repeat some steps that are mentioned in the above syntax. After that, we need to find the midpoint and compare the left value and right value as shown in the above syntax.How does Binary Search Work in Data Structure?
Now let’s see how binary search works in a data structure as follows.
For a binary search, we must need a sorted array or list to implement a binary search. We will become familiar with the process of binary search with a pictorial model. Coming up next is our arranged exhibit and allowed us to accept that we need to look through the area of significant worth 35 utilizing the binary search.
In figure show a sorted array or list with the position of value that we call the location, which we need to implement binary search because binary search works as linear search.
Now find the middle of this by using the following formula as follows.
MID_PT = START + END /2
Here, START is 0 and END is 9
So we take an integer value that is 4 and this is the MID_PTof the list as shown below figure as follows.
Now we need to compare the searchvalue with location 4. See here at 4th location we got 30 and our search value is 35 that mean value does not match. If the search value is less than MID_PT then we search value on the left side and if the value is greater than MID_PT then we search value on the right side because we know the list is sorted. So in this example, we need to search the value on the right side as shown below figure.
Now change the value of START and MID_PT as follows.
START = MID_PT + 1
MID_PT = START + END /2
Then we got a newMID_PT that is 7, now compare the search value with MID_PT, see here search value is less than MID_PT.
Note here search value is 35 less than MID_PT which means we need to follow the same process that is found MID_PT and check if the value is less or greater. To conclude our search value 35 we got at the 5th location as shown in the below figure as follows.Examples of Binary Search in Data Structure
Now see an example of binary search by using a python programming language as follows. Normally we can implement binary search by using different programming languages but here we try to implement by using python programming as follows.Example #1
print(“search value not found”)
Explanation: In the above example we use python programming language, in this example, we follow the same steps that are mentioned in the above point. End output of the above code we illustrated by using the following screenshot as follows.
In the above screenshot, we try to search value 35 if the value is found in a static list then it shows the message search value is found at the location as shown in the above screenshot. If I need to search 5 value but this value is not present in the list then it shows message search value not found as shown in the below screenshot as follows.Example #2
Similarly, we can implement binary search by using c programming language as follows.
Explanation: By using the above c code we try to implement binary search in the data structure, so in this way wecan use any programming language to implement binary search. End output of the above code we illustrated by using the following screenshot as follows.Conclusion
We hope from this article you learn the Binary Search in Data structure. From the above article, we have learned the basic syntax of Binary Search and we also see different examples of Binary Search. From this article, we learned how and when we use the Binary Search in Data structure.Recommended Articles
This is a guide to Binary Search in Data Structure. Here we also discuss the introduction and syntax of binary search in a data structure along with different examples and its code implementation. You may also have a look at the following articles to learn more –
You're reading Binary Search In Data Structure
Update the detailed information about Binary Search In Data Structure on the Phuhoabeautyspa.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!