Você está na página 1de 18

Data Structures and Algorithm Analysis

Searching Part 1
Prepared by : Harprith Kaur Randhawa
1

Searching
Searching is the process of finding the location of a target among a list of objects. There are two basic searching methods: sequential and binary search.
2

Searching
Sequential Search
The sequential search is normally used when an array is not sorted. It starts at the beginning of the array and searches until it finds the data or hits the end of the list.

Sequential Search Algorithm


Algorithm seqsearch(val list val last val target <array> <index> <keytype>)

Locate the target in an ordered list of elements Pre List must contain at least one element last represents the last index target contains the data to be located Post if found assign found to true if not found assign found to false 1. looker = 0 2. loop ( looker <= last AND target not equal list[looker] ) 2.1 looker = looker + 1 3. if ( target equal list[looker]) 3.1 found = true else 3.2 found = false 4. return found end seqsearch

Variations on sequential searches


Sentinel Search
Probability Search Ordered Search

Sentinel Search
Algorithm sentinelsearch( val list <array> val last <index> val target <keytype>) 1. list[last + 1] = target 2. looker = 0 3. loop (target not equal list[looker] ) looker = looker + 1 4 if ( looker <= last ) found = true else found = false 5. return found end sentinelsearch

What is the difference between sentinel search and general sequential search ???
6

Probability Search
Algorithm probabilitylsearch( val list <array> val last <index> val target <keytype> ) looker = 0 loop ( looker <= last and target not equal list[looker]) looker = looker + 1 if ( target equal list[looker] ) found = true What is the if ( looker > 0) temp = list[looker 1] difference list[looker 1] = list[looker] between list[looker] = temp looker = looker - 1 probability search else and general found = false return found sequential search end probabilitysearch

???

Ordered Search
Algorithm orderedsearch(val list val last val target if ( target < = list[last]) looker = 0 loop ( target > list[looker]) looker = looker + 1 2. else looker = last 3. if ( target equal list[looker]) found = true 4. else found = false return found end of orderedsearch <array> <index> <keytype>)

What is the difference between ordered search and general sequential search ???
8

Exercise:

Convert the sequential search algorithm into a program code. You may use C++ or JAVA as the programming language.

Solution
import javax.swing.JOptionPane; public class Seq { public static void main(String args[]) { int list[] = {7,8,5,4,3,2,1,9,6,10};

int last = 9;
int target; target = Integer.parseInt(JOptionPane.showInputDialog("E nter target value")); if (seqsearch(list,last,target) ) JOptionPane.showMessageDialog(null, "The value was found in list "); else JOptionPane.showMessageDialog(null, "The value is not in the list");

static boolean seqsearch(int list[], int last, int target) { int looker = 0; boolean found; while ( looker <= last && target != list[looker]) ++looker;

if( target == list[looker]) found = true; else found = false; return found; } }

10

Binary Search
The sequential search algorithm is very slow. If we have an array of 1000 elements, we must do 1000 comparisons in the worst case. If the array is not sorted, the sequential search is the only solution. If the array is sorted, we can use a more efficient algorithm called the binary search. The binary search starts by testing the data in the element at the middle of the array to determine if the target is in the first or second half of the list. If it is in the first half of the list, we do not need to check the second half. 11

Binary Search Algorithm


Algorithm binarysearch ( val list <array> val end <index> val target <keytype>) Search an ordered list using Binary Search Pre list is ordered, it must contain at least one element end is index to the largest element in the list target is the value of element being sought

Post FOUND :Found set true NOT FOUND :Found is set to false Return found
12

Binary Search Algorithm


first = 0 last = end loop ( first <= last) mid = ( first + last) / 2 if ( target > list[mid] ) first = mid + 1 3. else if ( target < list[mid] ) last = mid 1 4. else first = last + 1 5.end if 4 end loop

5. if ( target equal list [mid] ) found = true 6. else found = false


7. end if 8. return found end binarysearch
13

ALGORITHM EFFICIENCY

Conclusion :
Which searching technique is more efficient ????

14

Analyzing Search Algorithms


The basic loop for the sequential search is shown as below: while ( looker < last && target != list[looker] ++looker; This is a linear algorithm. This search is known as a linear search because the algorithm is linear, so its efficiency is O(n).
15

Analyzing Search Algorithms


The binary search locates an item by repeatedly dividing the list in half. Its loop is
while ( first <= last) { mid = ( first + last) / 2; if ( target > list[mid] ) first = mid + 1 else if ( target < list[mid]) last = mid 1; else first = last + 1;

16

ALGORITHM EFFICIENCY
This loops divides and it is therefore a logarithmic loop. So, the efficiency is thus

O( log 2n)

17

Exercise
Write a complete program that will implement binary search to search an element stored in an array.

You may write your program in C++ or JAVA code.

18

Você também pode gostar