Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Binary Search Using Iteration

The document explains the Binary Search algorithm, detailing its implementation both iteratively and recursively, along with an example of searching for an element in a sorted array. It outlines the time complexities for best, average, and worst cases as O(1) and O(log n), and discusses space complexities for both approaches. The document concludes with a summary of key points regarding the algorithm's efficiency and methodology.

Uploaded by

Himadri Bhardwaj
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Binary Search Using Iteration

The document explains the Binary Search algorithm, detailing its implementation both iteratively and recursively, along with an example of searching for an element in a sorted array. It outlines the time complexities for best, average, and worst cases as O(1) and O(log n), and discusses space complexities for both approaches. The document concludes with a summary of key points regarding the algorithm's efficiency and methodology.

Uploaded by

Himadri Bhardwaj
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Binary Search Algorithm

• Suppose, we are given the elements as:


30, 20, 60, 3, 40, 15, 10
• The first step is to sort the elements as:
3, 10,15, 20, 35, 40, 60
A[0] A[1] A[2] A[3] A[4] A[5] A[6]
3 10 15 20 35 40 60
0 1 2 3 4 5 6

Let the element to be searched is 15


1. We take the beg=0 (first index number), end=6 (last index
number) and compute location of the middle element as
mid=(beg+end)/2 = (0+6)/2 = 3

2. Compare the item with mid i.e. a[mid]= a[3] is not equal to 15
A[0] A[1] A[2] A[3] A[4] A[5] A[6]

3 10 15 20 35 40 60
0 1 2 3 4 5 6

beg mid end

Start the next iteration


As a[mid]=20 > 15, therefore, we take
end = mid - 1 = 3-1 = 2
On the other hand, beg remains the same
Therefore,
mid = (beg + end) / 2 = (0+2) / 2 = 1
A[0] A[1] A[2] A[3] A[4] A[5] A[6]

3 10 15 20 35 40 60
0 1 2 3 4 5 6

beg mid end

Now, a[mid] i.e. a[1]=10 < 15.

Therefore, we take
beg = mid + 1 = 1+1 = 2

The end remains same, i.e. end = 2

Since beg=end

Compute the mid element

mid=(beg+end)/2=(2+2)/2=2

Since a[mid] i.e. a[2]=15, the search terminates on success.


Implementation of Binary Search using Iterative Approach

#include<stdio.h>
int BSearch(int a[], int selement, int low, int high)
{
int mid ;
while (low <= high)
{
mid = low + (high - low) / 2;
if (a[mid] == selement)
return mid;
else if (a[mid] < selement)
low = mid + 1;
else
high = mid - 1;
}
return -99;
}
Cont.
int main(void)
{
int number;
int a[] = {2, 3, 4, 5, 6, 9, 11, 12, 23};
int status ;
int selement;
printf("\n Enter the element to be searched: ");
scanf("%d", &selement);
status = BSearch(a, selement, 0, number - 1);
if (status == -99)
printf("\n Element not found");
else
printf("Element is found at index %d", status);
return 0;
}
Binary Search using Recursion
#include <stdio.h>
int BSearch(int a[], int selement, int low, int high)
{
int mid;
if (high >= low)
{
mid = low + (high - low) / 2;

if ( a[mid] == selement )
return mid;
else if (a[mid] > selement) // Search the left portion
return BSearch(a, selement, low, mid - 1);
else // Search the right portion
return BSearch(a, selement, mid + 1, high);
}
return -99;
}
Cont.
int main()
{
int a[] = {2, 3, 4, 5, 6, 9, 11, 12, 23};
int low = 0;
int high = 8;
int element;
int status;
printf("\n Enter the element to be searched: ");
scanf("%d", &element);
status = BSearch(a, element, low, high );
if(status == -99)
printf("\n Element not found: ");
else
printf("\n The element %d is present at index no %d", element, s);
return 0;
}
Time Complexity of Binary Search
Best Case Time Complexity of Binary Search

• The best case scenario of Binary Search occurs when


the target element is in the central index. In this
situation, there is only one comparison.

• Therefore, the Best Case Time Complexity of Binary


Search is O(1).
Average Case Time Complexity of Binary Search

• The average case arises when the target element is present in some
location other than the central index or extremities. The time
complexity depends on the number of comparisons to reach the
desired element.
• In the following iterations, the size of the sub array is reduced using
the result of the previous comparison.
Initial length of array = n
n
Iteration1 : Length of array 
2
n
2 n
Iteration 2 : Length of array   2
2 2

n
Iteration k : Length of array 
2k
Cont.

• After k iterations, the size of the array


becomes 1 (narrowed down to the first element or last
element only).
• Length of array = n/2k = 1
=> n = 2k
Applying log function on both sides:
=> log2(n) = log2(2k)
=> log2(n) = k ∗ log22 = k
=> k = log2(n)
• Therefore, the overall Average Case Time Complexity of
Binary Search is O (log n).
Worst Case Time Complexity of Binary Search
• The worst-case scenario of Binary Search occurs when the
target element is the smallest element or the largest
element of the sorted array.

• Since the target element is present in the first or the last index,
i.e. there are log n comparisons in total.

• Therefore, the Worst-Case Time Complexity of Binary Search


is O(log n).
Space Complexity of Binary Search
For Iterative Approach:
• In the case of the iterative approach, no extra space is used.
Hence, the space complexity is O(1).

For Recursive Approach


• In the worst case, log n recursive calls are stacked in the memory.
– k comparisons require k recursive calls to be stacked in the
memory.
– Since average time complexity analysis has log
n comparisons, the average memory will be O(log n).
• Thus, in recursive implementation the overall space complexity
will be O( log n).
Summary
• Binary Search is used for finding the location of an element in
a linear array.
• Binary search uses the divide and conquer technique.
• The time complexity of binary search as follows:
– Best Case – O(1)
– Worst Case – O(log n)
– Average Case – O(log n)
• The space complexity for binary search is
– recursive approach – O(log n)
– iterative approach – O(1)

You might also like