Back to Course


0% Complete
0/82 Steps
  1. Getting Started with Algorithm
    What is an Algorithm?
  2. Characteristics of Algorithm
    1 Topic
  3. Analysis Framework
  4. Performance Analysis
    3 Topics
  5. Mathematical Analysis
    2 Topics
  6. Sorting Algorithm
    Sorting Algorithm
    10 Topics
  7. Searching Algorithm
    6 Topics
  8. Fundamental of Data Structures
  9. Queues
  10. Graphs
  11. Trees
  12. Sets
  13. Dictionaries
  14. Divide and Conquer
    General Method
  15. Binary Search
  16. Recurrence Equation for Divide and Conquer
  17. Finding the Maximum and Minimum
  18. Merge Sort
  19. Quick Sort
  20. Stassen’s Matrix Multiplication
  21. Advantages and Disadvantages of Divide and Conquer
  22. Decrease and Conquer
    Insertion Sort
  23. Topological Sort
  24. Greedy Method
    General Method
  25. Coin Change Problem
  26. Knapsack Problem
  27. Job Sequencing with Deadlines
  28. Minimum Cost Spanning Trees
    2 Topics
  29. Single Source Shortest Paths
    1 Topic
  30. Optimal Tree Problem
    1 Topic
  31. Transform and Conquer Approach
    1 Topic
  32. Dynamic Programming
    General Method with Examples
  33. Multistage Graphs
  34. Transitive Closure
    1 Topic
  35. All Pairs Shortest Paths
    6 Topics
  36. Backtracking
    General Method
  37. N-Queens Problem
  38. Sum of Subsets problem
  39. Graph Coloring
  40. Hamiltonian Cycles
  41. Branch and Bound
    2 Topics
  42. 0/1 Knapsack problem
    2 Topics
  43. NP-Complete and NP-Hard Problems
    1 Topic
In Progress
Lesson 15 of 43
In Progress

Binary Search

Let ai, 1<= i <= n, be a list of elements that are sorted in non decreasing order. Consider the problem of determining whether a given element x is present in the list.If x is present,we are to determine a value j such that aj = x. If x is not in the list, then j is to be set to zero. Let P = (n,ai,…, al,x) denote an arbitrary instance of this search problem(n is the number of elements in the list ai,…, al is the list of elements,and x is the element searched for)

In this example,any given problem P gets divided (reduced)into one new subproblem. This division takes only theta(1) time. After a comparison with aq, the instance remaining to be solved (if any) can be solved by using this divide-and-conquer scheme again.If q is always chosen such that aq is the middle element(that is,q = [(n +l)/2)], then the resulting search algorithm is known as binary search.Note that the answer to the new subproblem is also the answer to the original problem P; there is no need for any combining. Algorithm 2 describes this binary search method, where BinSrch has four inputs a[], i, l and x. It is initially invoked as BinSrch(a,l,n,x).

KodNest Capture28
Algorithm 2: Recursive binary search

A non recursive version of BinSrch is given in Algorithm 3. Bin Search has three inputs a,n,and x. The while loop continues processing as long as there are more elements left to check.At the conclusion of the procedure 0 is returned if x is not present, or j is returned, such that a[j] = x.

KodNest Capture29
Algorithm 3: Iterative binary search

Is BinSearch an algorithm? We must be sure that all of the operations such as comparisons between x and a[mid] are well defined. The relational operators carry out the comparisons among elements of a correctly if these operators are appropriately defined. Does BinSearch terminate? We observe that low and high are integer variables such that each time through the loop either x is found or low is increased by at least one or high is decreased by at least one.Thus we have two sequences of integers approaching each other and eventually low becomes greater than high and causes termination in a finite number of steps if x is not present.

New Report