Back to Course

Algorithm

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
    Stacks
  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
Lesson 7, Topic 2
In Progress

Binary Search

Lesson Progress
0% Complete

Binary search is a fast search algorithm with run-time complexity of Ο(log n). This search algorithm works on the principle of divide and conquer. For this algorithm to work properly, the data collection should be in the sorted form.

Binary search looks for a particular item by comparing the middle most item of the collection. If a match occurs, then the index of item is returned. If the middle item is greater than the item, then the item is searched in the sub-array to the left of the middle item. Otherwise, the item is searched for in the sub-array to the right of the middle item. This process continues on the sub-array as well until the size of the subarray reduces to zero.

Binary Search Algorithm

  • Step 1: [INITIALIZE] SET BEG = lower_bound
    END = upper_bound, POS = – 1
  • Step 2: Repeat Steps 3 and 4 while BEG <=END
  • Step 3: SET MID = (BEG + END)/2
  • Step 4: IF A[MID] = VAL
    SET POS = MID
    PRINT POS
    Go to Step 6
    ELSE IF A[MID] > VAL
    SET END = MID – 1
    ELSE
    SET BEG = MID + 1
    [END OF IF]
    [END OF LOOP]
  • Step 5: IF POS = -1
    PRINT “VALUE IS NOT PRESENT IN THE ARRAY”
    [END OF IF]
  • Step 6: EXIT

Complexity

SNPerformanceComplexity
1Worst caseO(log n)
2Best caseO(1)
3Average CaseO(log n)
4Worst case space complexityO(1)

Example :

Let us consider an array arr = {1, 5, 7, 8, 13, 19, 20, 23, 29}. Find the location of the item 23 in the array.

In First step :

  1. BEG = 0   
  2. END = 8 
  3. MID = 4   
  4. a[mid] = a[4] = 13 < 23, therefore  

In Second step:

  1. Beg = mid +1 = 5   
  2. End = 8  
  3. mid = 13/2 = 6    
  4. a[mid] = a[6] = 20 < 23, therefore;   

In Third step:

  1. beg = mid + 1 = 7   
  2. End = 8   
  3. mid = 15/2 = 7  
  4. a[mid] = a[7]   
  5.  a[7] = 23 = item;   
  6. therefore, set location = mid;   
  7. The location of the item will be 7.   
This image has an empty alt attribute; its file name is 22-1.png

Binary Search Program using Recursion

C program

#include<stdio.h>
int binarySearch(int[], int, int, int);
void main ()
{
    int arr[10] = {16, 19, 20, 23, 45, 56, 78, 90, 96, 100};
    int item, location=-1;
    printf("Enter the item which you want to search ");
    scanf("%d",&item);
    location = binarySearch(arr, 0, 9, item);
    if(location != -1)
    {
        printf("Item found at location %d",location);
    }
    else
    {
        printf("Item not found");
    }
}
int binarySearch(int a[], int beg, int end, int item)
{
    int mid;
    if(end >= beg)
    {
        mid = (beg + end)/2;
        if(a[mid] == item)
        {
            return mid+1;
        }
        else if(a[mid] < item)
        {
            return binarySearch(a,mid+1,end,item);
        }
        else
        {
            return binarySearch(a,beg,mid-1,item);
        }
    }
    return -1;
}  

Output:

Enter the item which you want to search
19
Item found at location 2

Binary Search function using Iteration

int binarySearch(int a[], int beg, int end, int item)
{
    int mid;
    while(end >= beg)
    {
        mid = (beg + end)/2;
        if(a[mid] == item)
        {
            return mid+1;
        }
        else if(a[mid] < item)
        {
            beg = mid + 1;
        }
        else
        {
            end = mid - 1;
        }
    }
    return -1;
}   
KodNest Training New Batch is starting on 02nd November 2020. Attend one week free demo classes.Register Now

New Report

Close