Software Development Exam  >  Software Development Notes  >  Basics of C++  >  Code: Binary Search

Code: Binary Search | Basics of C++ - Software Development PDF Download

Algorithm

Parameters inital_value , end_value

  • Step 1 : Find the middle element of array. using ,
  • middle = initial_value + end_value / 2 ;
  • Step 2 : If middle = element, return ‘element found’ and index.
  • Step 3 : if middle > element, call the function with end_value = middle - 1 .
  • Step 4 : if middle < element, call the function with start_value = middle + 1 .
  • Step 5 : exit.

The implementation of the binary search algorithm function uses the call to function again and again. This call can be of two types

  • Iterative call is looping over the same block of code multiple times
  • Recursive call is calling the same function again and again.

Program to Implement Binary Search Using Iterative Call

Example

#include <stdio.h>

int iterativeBinarySearch(int array[], int start_index, int end_index, int element){

   while (start_index <= end_index){

      int middle = start_index + (end_index- start_index )/2;

      if (array[middle] == element)

         return middle;

      if (array[middle] < element)

         start_index = middle + 1;

      else

         end_index = middle - 1;

   }

   return -1;

}

int main(void){

   int array[] = {1, 4, 7, 9, 16, 56, 70};

   int n = 7;

   int element = 16;

   int found_index = iterativeBinarySearch(array, 0, n-1, element);

   if(found_index == -1 ) {

      printf("Element not found in the array ");

   }

   else {

      printf("Element found at index : %d",found_index);

   }

   return 0;

}

Output

Element found at index : 4

Program to Implement Binary Search Using Recursive Call

Example:

#include <stdio.h>

int recursiveBinarySearch(int array[], int start_index, int end_index, int element){

   if (end_index >= start_index){

      int middle = start_index + (end_index - start_index )/2;

      if (array[middle] == element)

         return middle;

      if (array[middle] > element)

         return recursiveBinarySearch(array, start_index, middle-1, element);

      return recursiveBinarySearch(array, middle+1, end_index, element);

   }

   return -1;

}

int main(void){

   int array[] = {1, 4, 7, 9, 16, 56, 70};

   int n = 7;

   int element = 9;

   int found_index = recursiveBinarySearch(array, 0, n-1, element);

   if(found_index == -1 ) {

      printf("Element not found in the array ");

   }

   else {

      printf("Element found at index : %d",found_index);

   }

   return 0;

}

Output


Element found at index : 3

The document Code: Binary Search | Basics of C++ - Software Development is a part of the Software Development Course Basics of C++.
All you need of Software Development at this link: Software Development
70 videos|45 docs|15 tests

Top Courses for Software Development

70 videos|45 docs|15 tests
Download as PDF
Explore Courses for Software Development exam

Top Courses for Software Development

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

video lectures

,

Code: Binary Search | Basics of C++ - Software Development

,

Sample Paper

,

Previous Year Questions with Solutions

,

shortcuts and tricks

,

ppt

,

Code: Binary Search | Basics of C++ - Software Development

,

mock tests for examination

,

past year papers

,

Summary

,

Extra Questions

,

Viva Questions

,

Semester Notes

,

Code: Binary Search | Basics of C++ - Software Development

,

Exam

,

MCQs

,

study material

,

pdf

,

practice quizzes

,

Objective type Questions

,

Free

,

Important questions

;