0% found this document useful (0 votes)
17 views32 pages

Lec 3- Searching

Uploaded by

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

Lec 3- Searching

Uploaded by

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

Data Structure and Algorithm

CSC - 220

Lecture 3

Searching Algorithms

Dr. Tariq Ali


Searching
• Searching is an operation or a technique that
helps to find the place of a given element or
value in the list.
– It can be successful or unsuccessful
• Most common and standard techniques are
– Linear (Sequential or brute-force) Search
– Binary Search
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 will be
• Bottom line: you don’t always need to use or shouldn’t
use the fastest algorithm.
Searching in an Ordered Collection

• A search traverses the collection until


– The desired element is found
– Or the collection is exhausted
• If the collection is ordered, it might not
have to look at all elements
– I can stop looking when I know the
element cannot be in the collection.
Searching in an Unordered Collection

• Let’s determine if the value 12 is in the


collection:

Head
35 42 12 5 \\

12 Found!
Searching in an Unordered Collection

• Let’s determine if the value 13 is not in the


collection:

Head
35 42 12 5 \\

13 Not Found!
Searching Algorithms
• Looking up a phone number, accessing a website and
checking a word’s definition in a dictionary all involve
searching through large amounts of data.
• Searching algorithms all accomplish the same goal,
finding an element that matches a given search key
• The major difference is the amount of effort they
require to complete the search.
• One way to describe this effort is with Big O notation.
– For searching and sorting algorithms, this is particularly
dependent on the number of data elements.
Linear (Sequential) Search
• Linear search is a very basic and simple algorithm
• We search an element or value in a given array
by traversing the array from the starting till
– desired element or
– value is found.
• It compares the required element with all the
elements of the array and when the element is
matched successfully,
– It returns the index of the element in the array, else
– it return -1. (If unsuccessful)
Linear Search
• To determine that a value is not in the array,
– the program must compare the search key to
every array element.
• Linear search works well for
– Small or
– Unsorted arrays.
• However, for large arrays, linear search is
inefficient.
Note: linear search can be applied to both sorted and
unsorted arrays.
Linear Search on an Unordered File
• Basic algorithm:
Get the search criterion (key)
Get the first record from the file
While ( (record != key) and (still more records) )
Get the next record
End_while

• When do we know that there wasn’t a record in


the file that matched the key?
• Linear search works with O(n) Efficiency (worst case)
Linear Search on an Ordered File
• Basic algorithm:
Get the search criterion (key)
Get the first record from the file
While ( (record < key) and (still more records) )
Get the next record
End_while
If ( record = key )
Then success
Else there is no match in the file
End_else
• When do we know that there wasn’t a record in the
file that matched the key?
Sequential Search – 5
Example 1

Sequential Search – Example 1


0 1 2 3 4 5 6 7 8
X 10 5 17 7 12 14 25 15 3

Search for key = 25.

0 1 2 3 4 5 6 7 8
10 5 17 7 12 14 25 15 3

 Return 6
 The item is
found
Sequential Search – 6
Example 2

Sequential Search – Example 2


0 1 2 3 4 5 6
7 8
10 5 17 7 12 14 25 15 3
X

Search for key = 16.

0 1 2 3 4 5 6 7 8
10 5 17 7 12 14 25 15 3

 Return -1
 The item is not
found
Sequential Search Algorithms – 7
Method 1

Sequential Search Algorithm


 Method 1: Returns true or
false.
static bool SeqSearch(int[] arr, int sValue)
{
for(int i =0; i < arr.Length; i++)
if(arr[i] == sValue)
return true;
return false;
}
Sequential Search Algorithms – 8
Method 2

Sequential Search Algorithm

 Method 2: Returns positive or


negative.

static int SeqSearch(int[] arr, int sValue)


{
for(int i =0; i < arr.Length; i++)
if(arr[i] == sValue)
return i;
return -1 ;
}
Searching for Minimum and 9
Searching for Minimum (Maximum)
Maximum Values

Values

① Assign the first element of the array to a variable as the


min (max)
value.

② Begin looping through the array, comparing each


successive array element with the min (max) value variable.

③ If the currently accessed array element is less (more) than


the min (max) value, assign this element to the min (max)
value variable.

④ Continue until the last array element is accessed.

⑤ The min (max) value is stored in the variable.


Searching for Minimum and Maximum Values – 10
FindMin Method

FindMin Method

static int FindMin(int[] arr)


{
int min = arr[0] ;
for(int i =0 ; i < arr.Length; i++)
if(arr[i] < min)
min = arr[i];
return min;
}
Searching for Minimum and Maximum Values – 1
FindMax Method 1

FindMax Method

static int FindMax(int[] arr)


{
int max = arr[0];
for(int i =0; i < arr.Length; i++)
if(arr[i] > max)
max = arr[i];
return max;
}
Binary search
• Works only with sorted arrays
• Starts comparing the middle most item of the collection
• If a match occurs, then the index of item is returned.
• If the middle item is greater than the item, then the item is
searched in the sub-array to the left of the middle item.
• Otherwise, the item is searched for in the sub-array to the
right of the middle item.
• Process continues until the size of the subarray reduces to
zero
• Binary search, also known as half-interval search,
Binary Search
• Binary search uses a Recursive Method to search an
array to find a specified value
• The array must be a sorted array:
a[0]≤a[1]≤a[2]≤. . . ≤ a[finalIndex]
• If the value is found, its index is returned
• If the value is not found, -1 is returned

Note: Each execution of the recursive method reduces


the search space by about a half
Binary Search
• If the value looked for is larger than the value in the middle of
the array or array segment
– Then the first half of the array or array segment can be ignored
– This strategy is then applied to the second half of the array or array
segment
• If the value looked for is at the middle of the array or array
segment, then it has been found
• If the entire array (or array segment) has been searched in
this way without finding the value, then it is not in the array

• Binary search works with O(log n) Efficiency


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 ?
Binary Search – 19
Example 2
Binary Search – Example 2
Search for b =
0 1 2 3 4 5 6 7 8
22 10 11 12 13
9 14 15

 Return (the element is


10 found)
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 ?
– 1st try - list has 16 items
– 2nd try - list has 8 items
– 3rd try - list has 4 items
– 4th try - list has 2 items
– 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 - 63 items  2nd try - 128 items
 3rd try - 32 items  3rd try - 64 items
 4th try - 16 items  4th try - 32 items
 5th try - 8 items  5th try - 16 items
 6th try - 4 items  6th try - 8 items
 7th try - 2 items  7th try - 4 items
 8th try - 1 item  8th try - 2 items
 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!


Log 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 log 8 = 3
16 = 24 log 16 = 4

Note: There are no algorithms that run faster than ‘log n’ time.
Binary Search 17
Code

Binary Search Code

static int BinarySearch(int[] arr, int sValue)


{
int lower=0, upper = arr.Length-1, mid;
while(lower <= upper)
{
mid = (lower + upper)/2;
if(sValue == arr[mid])
return mid;
else if(sValue < arr[mid])
upper = mid-1;
else
lower = mid+1 ;
}
Return -1 ; // not found
}
Time & Space Complexity

Time Complexity

Space Complexity
O(1)
O(1)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy