0% found this document useful (0 votes)
8 views

Binary Search For Sorted Array

Uploaded by

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

Binary Search For Sorted Array

Uploaded by

Abdullah Muneer
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Binary search for

sorted array
Presented by : Abdullah Muneer
Roll#:2119
Introduction to Binary Search
• Binary search is an efficient algorithm for finding an
item from a sorted list of items. It works by repeatedly
dividing in half the portion of the list that could contain
the item, until narrowing down the possible locations to
just one.
Overview of Binary Search
• Binary search is a 'divide and conquer' algorithm. It
requires the array to be sorted and operates in O(log n)
time complexity. This makes it much more efficient than
linear search for large datasets.
How Binary Search Works
• 1.Initialize:Set low index to array’s start and high index
to array’s end.
2.Iterate:While low is less than or equal to high:
a)Calculate the middle index mid as (low+high/2).
b)If element is at the mid,then return mid.
c)If it’s less,set low to mid+1.
d)If it’s greater,set high to mid-1.

3.Return:If not found,then return -1.


Binary Search vs. Linear Search
• Linear Search:
- Time Complexity: O(n)
- Suitable for unsorted arrays
- Simple to implement

Binary Search:
- Time Complexity: O(log n)
- Requires sorted arrays
- More efficient for large datasets
Example:Array: [2, 3, 6, 8, 9, 13, 20]
Target: 13
Linear Search: Checks each element until it finds 13
Binary Search: Divides the array and finds 13 in fewer
steps
Real-world applications
• Binary search is widely used in:
- Database indexing
- Algorithm optimization
- Internet search engines
- Competitive programming
Performance analysis
• Time Complexity:
- Best Case: O(1)
- Average Case: O(log n)
- Worst Case: O(log n)

Space Complexity:
- O(1) for iterative approach
- O(log n) for recursive approach
Advantages and disadvantages
• Advantages:
- Efficient for large datasets
- Simple implementation
- Low memory usage with iterative approach

Disadvantages:
- Only works on sorted arrays
- Requires pre-sorting if array is unsorted
- Less effective for small datasets

You might also like