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

PF Lec13 Searching

The document discusses searching and sorting algorithms, focusing on sequential (linear) search and binary search, as well as bubble sort and insertion sort. It explains the efficiency of these algorithms, particularly highlighting the speed advantages of binary search for ordered lists. Additionally, it provides C++ code examples for implementing both iterative and recursive binary search methods.

Uploaded by

fahadhaider336
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

PF Lec13 Searching

The document discusses searching and sorting algorithms, focusing on sequential (linear) search and binary search, as well as bubble sort and insertion sort. It explains the efficiency of these algorithms, particularly highlighting the speed advantages of binary search for ordered lists. Additionally, it provides C++ code examples for implementing both iterative and recursive binary search methods.

Uploaded by

fahadhaider336
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Searching

Dr. Fahad
Searching and Sorting

Topics
• Sequential Search (Linear Search)
• Binary Search
• Bubble Sort
• Insertion Sort

Reading
• Sections 6.6 - 6.8
Common Problems

• There are some very common problems that we use


computers to solve:
o Searching through a lot of records for a specific record or set of
records
o Placing records in order, which we call sorting
• There are numerous algorithms to perform searches and
sorts. We will briefly explore a few common ones.
Searching
• A question you should always ask when selecting a
search algorithm is “How fast does the search have
to be?” The reason is that, in general, the faster
the algorithm is, the more complex it is.
• Bottom line: you don’t always need to use or
should use the fastest algorithm.
• Let’s explore the following search algorithms,
keeping speed in mind.
o Sequential (linear) search

o Binary search
Searching Arrays: Linear Search

• Search array for a key value

• Linear search
o Compare each element of array with key value
– Start at one end, go to other

o Useful for small and unsorted arrays


– Inefficient
– If search key not present, examines every element

02-Jan-24
Example (Linear Search)
void main (){
const int size=10;
int a[size]={ 5,6,1,8,3,7,2,10,9,4};
int searchKey;
cout << "Enter integer search key: ";
cin >> searchKey;
for( k=0; k<size; k++)
if (a[k]==searchKey){
cout<<"The element to be searched is found at index
a["<<k<<"]";
break;
}
If (k==size)
cout<<"The element to be searched is NOT found;
}
Binary Search

• If we have an ordered list and we know


how many things are in the list (i.e., number
of records in a file), we can use a different
strategy.
• The binary search gets its name because
the algorithm continually divides the list into
two parts.
How a Binary Search Works

Always look at the


center value. Each
time you get to discard
half of the remaining
list.

Is this fast ?
How Fast is a Binary Search?

• Worst case: 11 items in the list took 4 tries


• How about the worst case for a list with 32
items ?
o 1st try - list has 16 items
o 2nd try - list has 8 items
o 3rd try - list has 4 items
o 4th try - list has 2 items
o 5th try - list has 1 item
How Fast is a Binary Search? (con’t)

List has 250 items List has 512 items

1st try - 125 items 1st try - 256 items


2nd try - 128 items
2nd try - 63 items
3rd try - 64 items
3rd try - 32 items
4th try - 32 items
4th try - 16 items 5th try - 16 items
5th try - 8 items 6th try - 8 items
6th try - 4 items 7th try - 4 items
7th try - 2 items 8th try - 2 items
8th try - 1 item 9th try - 1 item
What’s the Pattern?

• List of 11 took 4 tries


• List of 32 took 5 tries
• List of 250 took 8 tries
• List of 512 took 9 tries

• 32 = 25 and 512 = 29
• 8 < 11 < 16 23 < 11 < 24
• 128 < 250 < 256 27 < 250 < 28
A Very Fast Algorithm!

• How long (worst case) will it take to find an


item in a list 30,000 items long?
210 = 1024 213 = 8192
211 = 2048 214 = 16384
212 = 4096 215 = 32768

• So, it will take only 15 tries!


Lg n Efficiency

• We say that the binary search algorithm


runs in log2 n time. (Also written as lg n)
• Lg n means the log to the base 2 of some
value of n.
• 8 = 23 lg 8 = 3 16 = 24 lg 16 = 4
• There are no algorithms that run faster than
lg n time.
Binary Search in C++ (Iterative method)

int binarySearch(int array[], int x, int low, int high) {


while (low <= high) { // Repeat until the pointers low and high meet
each other
int mid = low + (high - low) / 2;
if (array[mid] == x)
return mid;
if (array[mid] < x)
low = mid + 1;
else
high = mid - 1;
}
return -1;
}
Binary Search in C++ (Recursive method)

int binarySearch(int array[], int x, int low, int high) {


if (high >= low) {
int mid = low + (high - low) / 2;
if (array[mid] == x)
return mid; // If found at mid, then return it
if (array[mid] > x)
return binarySearch(array, x, low, mid - 1); // Search the left half
return binarySearch(array, x, mid + 1, high); // Search the right half
}
return -1;
}
Main function to call the Binary search

#include <iostream>
using namespace std;
int binarySearch(int array[], int x, int low, int high);
int main() {
int array[] = {3, 4, 5, 6, 7, 8, 9};
int x = 4;
int n = sizeof(array) / sizeof(array[0]);
int result = binarySearch(array, x, 0, n - 1);
if (result == -1)
cout<<"Not found";
else
cout<<"Element is found at index ” << result;
}
The end

You might also like