Академический Документы
Профессиональный Документы
Культура Документы
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.
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
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
Post FOUND :Found set true NOT FOUND :Found is set to false Return found
12
ALGORITHM EFFICIENCY
Conclusion :
Which searching technique is more efficient ????
14
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.
18