| Learn | Algorithm - Searching - Binary search |
|---|
Binary Search IntroductionBinary Search is a Divide and Conquer search algorithm. It uses Binary Search works by halving the search space at each iteration after comparing the target value to the middle value of the search space. To use Binary Search, the search space must be ordered (sorted) in some way. Duplicate entries (ones that compare as equal according to the comparison function) cannot be distinguished, though they don't violate the Binary Search property. Conventionally, we use less than (<) as the comparison function. If a < b, it will return true. if a is not less than b and b is not less than a, a and b are equal. Example QuestionYou are an economist, a pretty bad one though. You are given the task of finding the equilibrium price (that is, the price where supply = demand) for rice. Remember the higher a price is set, the larger the supply and the lesser the demand As your company is very efficient at calculating market forces, you can instantly get the supply and demand in units of rice when the price of rice is set at a certain price Your boss wants the equilibrium price ASAP, but tells you that the equilibrium price can be a positive integer that is at most You are allowed to call functions Example ExplanationHere our search space is from However, notice that as the The following psuedocode demonstrates the usage of Binary Search: This algorithm runs in C Implementation of Binary Search with Recursion |
Read Article
https://develop.horje.com/learn/1434/reference