0% found this document useful (0 votes)
101 views24 pages

DAA - Searching Techniques

The document discusses searching algorithms. It begins by introducing the problem of searching through a list of records to find a particular key. It then describes linear search, which searches through the records sequentially until the target key is found or all records are examined. Linear search takes O(n) time in the average and worst cases. The document then introduces binary search, which only works on sorted data. Binary search divides the search space in half at each step, requiring O(log n) time, making it faster than linear search. An example of binary search is provided.

Uploaded by

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

DAA - Searching Techniques

The document discusses searching algorithms. It begins by introducing the problem of searching through a list of records to find a particular key. It then describes linear search, which searches through the records sequentially until the target key is found or all records are examined. Linear search takes O(n) time in the average and worst cases. The document then introduces binary search, which only works on sorted data. Binary search divides the search space in half at each step, requiring O(log n) time, making it faster than linear search. An example of binary search is provided.

Uploaded by

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

Design and Analysis of

Algorithm
Topic: Searching Algorithms

Kannada Sangha Pune’s Kaveri College of Arts, Science and Commerce Mrs. Pallavi Joshi
Problem: Search
 We are given a list of records.
 Each record has an associated key.
 Give efficient algorithm for searching for a record containing a
particular key.
 Efficiency is quantified in terms of average time analysis (number of
comparisons) to retrieve an item.

Kannada Sangha Pune’s Kaveri College of Arts, Science and Commerce Mrs. Pallavi Joshi
Linear Search
 Step through array of records, one at a time.
 Look for record with matching key.
 Search stops when
 record with matching key is found
 or when search has examined all records without success.

Kannada Sangha Pune’s Kaveri College of Arts, Science and Commerce Mrs. Pallavi Joshi
Linear Search Algorithm
 

Kannada Sangha Pune’s Kaveri College of Arts, Science and Commerce Mrs. Pallavi Joshi
How Long Does Linear Search Take?
Okay, great, now we know how to search.
But how long will our search take?
That’s really three separate questions:
 How long will it take in the best case?
 How long will it take in the worst case?
 How long will it take in the average case?

Kannada Sangha Pune’s Kaveri College of Arts, Science and Commerce Mrs. Pallavi Joshi
Linear Search: Best Case
 In the best case, the target value is in the first element of the array.
 So the search takes some tiny, and constant, amount of time.
 Computer scientists denote this O(1).
 In real life, we don’t care about the best case, because it so rarely actually
happens.

Kannada Sangha Pune’s Kaveri College of Arts, Science and Commerce Mrs. Pallavi Joshi
Linear Search: Worst Case
 How long will our search take?
 In the worst case, the target value is in the last element of the array.
 So the search takes an amount of time proportional to the length of the array.
 Computer scientists denote this O(n).

Kannada Sangha Pune’s Kaveri College of Arts, Science and Commerce Mrs. Pallavi Joshi
Linear Search: Average Case
 In the average case, the target value is somewhere in the array.
 In fact, since the target value can be anywhere in the array, any element of the
array is equally likely.
 So on average, the target value will be in the middle of the array.
 So the search takes an amount of time proportional to half the length of the
array – also proportional to the length of the array – O(n) again!

 Can we do better?

Kannada Sangha Pune’s Kaveri College of Arts, Science and Commerce Mrs. Pallavi Joshi
Faster Search Requires Sorted Data
 Why not use linear search?
 Linear search means start at the beginning, and look at every piece of data until
you find your target.
 This is much much slower than the way you search a phonebook in real life.
 Why?
 The reason you can do the phonebook search so quickly is because the names in
the phonebook are sorted – specifically, they’re in alphabetical order by last
name, then by first name.

Kannada Sangha Pune’s Kaveri College of Arts, Science and Commerce Mrs. Pallavi Joshi
Binary Search
 The general term for a smart search through sorted data is a binary search.
1. The initial search region is the whole array.
2. Look at the data value in the middle of the search region.
3. If you’ve found your target, stop.
4. If your target is less than the middle data value, the new search region is the
lower half of the data.
5. If your target is greater than the middle data value, the new search region is
the higher half of the data.
6. Continue from Step 2.

Kannada Sangha Pune’s Kaveri College of Arts, Science and Commerce Mrs. Pallavi Joshi
Binary Search
 

Kannada Sangha Pune’s Kaveri College of Arts, Science and Commerce Mrs. Pallavi Joshi
Time Complexity of Binary Search
 How fast is binary search?
 Think about how it operates: after examining a value, the search region is cut in
half.
 So, for the first iteration of the loop, search region is the whole array.
 The second iteration, it’s half the array. (2nd iteration, it is (1/2))
 The third iteration, it’s a quarter of the array. (3rd iteration, it is (1/22))
 ...
 The kth iteration, it’s (1/2k-1) of the array.

Kannada Sangha Pune’s Kaveri College of Arts, Science and Commerce Mrs. Pallavi Joshi
Time Complexity of Binary Search
 For the kth iteration of the binary search loop, the search region is (1/2k-1) of the
array.
 What’s the maximum number of loop iterations?
log2n
 That is, we can’t cut the search region in half more than that many times.
 So, the time complexity of binary search is O(log2n).
 Therefore maximum recursion depth is floor(log2n) and worst case = O(log2n).
 Average case is also = O(log2n).

Kannada Sangha Pune’s Kaveri College of Arts, Science and Commerce Mrs. Pallavi Joshi
Binary Search

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Kannada Sangha Pune’s Kaveri College of Arts, Science and Commerce Mrs. Pallavi Joshi
Binary Search

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Find approximate midpoint

Kannada Sangha Pune’s Kaveri College of Arts, Science and Commerce Mrs. Pallavi Joshi
Binary Search

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Is 7 = midpoint key? NO.

Kannada Sangha Pune’s Kaveri College of Arts, Science and Commerce Mrs. Pallavi Joshi
Binary Search

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Is 7 < midpoint key? YES.

Kannada Sangha Pune’s Kaveri College of Arts, Science and Commerce Mrs. Pallavi Joshi
Binary Search

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Search for the target in the area before midpoint.

Kannada Sangha Pune’s Kaveri College of Arts, Science and Commerce Mrs. Pallavi Joshi
Binary Search

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Find approximate midpoint

Kannada Sangha Pune’s Kaveri College of Arts, Science and Commerce Mrs. Pallavi Joshi
Binary Search

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Target = key of midpoint? NO.

Kannada Sangha Pune’s Kaveri College of Arts, Science and Commerce Mrs. Pallavi Joshi
Binary Search

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Target < key of midpoint? NO.

Kannada Sangha Pune’s Kaveri College of Arts, Science and Commerce Mrs. Pallavi Joshi
Binary Search

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Target > key of midpoint? YES.

Kannada Sangha Pune’s Kaveri College of Arts, Science and Commerce Mrs. Pallavi Joshi
Binary Search

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Search for the target in the area after midpoint.

Kannada Sangha Pune’s Kaveri College of Arts, Science and Commerce Mrs. Pallavi Joshi
Binary Search

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Find approximate midpoint.


Is target = midpoint key? YES.
Kannada Sangha Pune’s Kaveri College of Arts, Science and Commerce Mrs. Pallavi Joshi

You might also like