Sie sind auf Seite 1von 30

Algorithms & Data Structure

(Searching Algorithms)
Common Problems - Reminder
There are some very common problems that we use
computers to solve:
Searching through a lot of records for a specific
record or set of records

Sorting, Placing records in order.


Searching
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.
Searching Algorithms
Necessary components to search in a list of data
Array containing the list
Length of the list
Item for which you are searching

After search completed


If item found, report success, return location in array
If item not found, report not found or failure
Searching Algorithms
Linear (Sequential) Searching Algorithm
Can deal with unsorted arrays

Binary Searching Algorithm


Must deal with sorted arrays
Linear (Sequential) Searching
Algorithm
Linear Search Algorithm

Start at first element of array.

Compare value to value (key) for which you are searching

Continue with next element of the array until you find a


match or reach the last element in the array.

Note: On the average you will have to compare the search


key with half the elements in the array.
Linear Search Algorithm
Suppose that you want to determine whether 27 is in the list

First compare 27 with list[0]; that is, compare 27 with 35

Because list[0] 27, you then compare 27 with list[1]

Because list[1] 27, you compare 27 with the next element in the list

Because list[2] = 27, the search stops

This search is successful!

Figure 1: Array list with seven (07) elements


Linear Search Algorithm
Lets now search for 10
The search starts at the first element in the list; that is, at list[0]
Proceeding as before, we see that this time the search item,
which is 10, is compared with every item in the list
Eventually, no more data is left in the list to compare with the
search item; this is an unsuccessful search
Linear Search Algorithm

public int linSearch(int[] list, int listLength, int key)


{
int loc;

for(int loc = 0; loc < listLength; loc++)


{
if(list[loc] == key)
return loc;
}
return -1;
}
Performance Analysis - Linear Search
Suppose that the first element in the array list contains the variable
key, then we have performed one comparison to find the key.

Suppose that the second element in the array list contains the
variable key, then we have performed two comparisons to find the
key.

Carry on the same analysis till the key is contained in the last element
of the array list. In this case, we have performed N comparisons (N is
the size of the array list) to find the key.

Finally if the key is NOT in the array list, then we would have
performed N comparisons and the key is NOT found and we would
return -1.
Performance Analysis - Linear Search
Therefore, the best case is: 1
And, the worst case is: N
The average case is:

Worst case and key found at the end of


the array list!

Best case

Average Number of
1 + 2 + 3 + ..+ N + N
=
Comparisons
N+1
Worst case and key is NOT found!
Number of possible cases
Performance Analysis - Linear Search

Therefore, average case time complexity


for Linear or sequential search is O(N).
Binary Searching Algorithm
Binary Search Algorithm

Can only be performed on a sorted list !!!

Uses divide and conquer technique to search list


Binary Search Algorithm

Search item is compared with middle element of list


If search item < middle element of list, search is
restricted to first half of the list
If search item > middle element of list, search second
half of the list
If search item = middle element, search is complete
Binary Search Example
list
search key = 19
0 1
1 5
2 15
3 19
4 25
5 27
6 29 middle of the array
7 31 compare list[6] and 19
8 33 19 is smaller than 29 so the next
9 45 search will use the lower half of the array
10 55
11 88
12 100
Binary Search Pass 2
list
search key = 19
0 1
1 5
2 15 use this as the middle of the array
3 19 Compare list[2] with 19
4 25
5 27 15 is smaller than 19 so use the top
half for the next pass
Binary Search Pass 3
search key = 19

list

3 19
4 use this as the middle of the array
25
5 Compare list[4] with 19
27
25 is bigger than 19 so use the bottom
half
Binary Search Pass 4
search key = 19

list

3 19 use this as the middle of the array


Compare list[3] with 19
Found!!
Binary Search Example
list
search key = 18
0 1
1 5
2 15
3 19
4 25
5 27
6 29 middle of the array
7 31 compare list[6] and 18
8 33 18 is smaller than 29 so the next
9 45 search will use the lower half of the array
10 55
11 88
12 100
Binary Search Pass 2
list
search key = 18
0 1
1 5
2 15 use this as the middle of the array
3 19 Compare list[2] with 18
4 25
5 27 15 is smaller than 18 so use the top
half for the next pass
Binary Search Pass 3
search key = 18

list

3 19
4 use this as the middle of the array
25
5 Compare list[4] with 18
27
25 is bigger than 18 so use the bottom
half
Binary Search Pass 4
search key = 18

list

3 19 use this as the middle of the array


Compare list[3] with 18
Does not match and no more elements
to compare.
Not Found!!
Binary Search Recursive Method
public int binarySearch(int list[ ], int first, int, last, int key)
{
if(first > last) return FALSE;

mid = (first + last) / 2;

if(key==list[mid]) return TRUE;

if(key<list[mid])
return binarySearch(list, first, mid-1, key);

if(key>list[mid])
return binarySearch(list, mid+1, last, key);

}
Analysis using the Recurrence equation
public int binarySearch(int list[ ], int first, int, last, int key)
{
if(first > last) return FALSE; Cost = C1

mid = (first + last) / 2; Cost = C2

if(key==list[mid]) return TRUE; Cost = C3


The same
if(key<list[mid]) problem of
return binarySearch(list, first, mid-1, key); size n/2

if(key>list[mid]) The same


return binarySearch(list, mid+1, last, key); problem of
size n/2
}
Performance Analysis - Binary Search

Then, the recurrence equation is:

T(n) = C1+C2+C3+T(n/2)

T(n) = C + T(n/2)

Therefore, you can solve it using iterative method or


recursive tree or Master Method.
Using Iterative method

By Expansion
Using Iterative method
We reach T(1) when

Replacing i in the formula


Using Master method

Solution

Case 2 (k=0)

Das könnte Ihnen auch gefallen