Binary search
Overview
In computer science, there are some problems that may repeat themselves in the daily job of a software engineer. one of the most common is searching through a collection of items, and a common approach to this kind of problem is the binary search implementation.
Why
When we are heading to solve any problem it is indeed important to know the rational reasons to approach the problem in one-way over another. think about a situation where you want to find a certain book in the library (Where all the books are already sorted alphabetically) you can go through the "array" of books one by one from the letter A until you get to the letter Z, you will finally find the book but does this kind of "searching algorithm" is efficient? well in computer science this kind of searching is called a linear search where you pass through any item in the array until you find your target, which is called also the time complexity of O(n), but what if our n is a million items or one billion? this kind of solution is very inefficient. with that in mind, we can use our binary search algorithm.
领英推荐
Conceptual understanding of binary search
The main concept of binary search is achieving a time complexity of O(log(n)) which is the opposite of exponential time complexity, where with each iteration we will shrink our array by half our solution becomes more efficient in the manner of time complexity. our implementation of the algorithm goes this way:
Conclusions
Implementing binary search is a key concept to understand, even though in most cases linear search with a simple "for loop" can get the wanted result but when we are dealing with a huge amount of data this kind of solution becomes somewhat inefficient. here is below an actual code implementation of binary search written in TypeScript.
WordPress Expert & Mentor | Empowering Web Success
7 个月???? ??? ?? ??????! ??? ????? ???? ?????? ?????? ????? ?????? ????? ??? ????? ??????? ?????? ?????? ?????? ??????: https://chat.whatsapp.com/BubG8iFDe2bHHWkNYiboeU
Backend Developer at MyGenes | C# | .NET | MSSQL
2 年???? ?????
I'm here to make great things happen??? | Product Manager | Content Creator | Idealist | Forever a student
2 年?????? ????? ?? ?????!
Software Developer
2 年??? ???? ?????? ? "????? ????? ????? ?????? ????? ??? ????? ??? ????? ?????? ???? ??????" ??? ???? ????? ???? ??? ?? ????? ??? ?????????? ??????? ???? ????? ?????? ??????? ???? ????? ?????? ?????? ??????, ??? ??????? ????? ??? ???? ?? ???? ????? ?? ?? ????? ??????.
Technical Support Engineer | book reader & nature lover.
2 年great article, well done! ??.