Lecture 3 (Stud Copy) - Searching
Transcript of Lecture 3 (Stud Copy) - Searching
-
8/3/2019 Lecture 3 (Stud Copy) - Searching
1/18
Data Structures andAlgorithm Analysis
Searching
Part 1Prepared by : Harprith Kaur Randhawa
1
-
8/3/2019 Lecture 3 (Stud Copy) - Searching
2/18
Searching
Searching is the process of findingthe location of a target among a listof objects.
There are two basic searching
methods: sequential and binary
search.
2
-
8/3/2019 Lecture 3 (Stud Copy) - Searching
3/18
Searching
Sequential Search
The sequential search is normally used
when an array is not sorted. It starts atthe beginning of the array and searchesuntil it finds the data or hits the end ofthe list.
3
-
8/3/2019 Lecture 3 (Stud Copy) - Searching
4/18
Sequential Search Algorithm
Algorithm seqsearch(val list
val last
val target )
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
-
8/3/2019 Lecture 3 (Stud Copy) - Searching
5/18
Variations on sequentialsearches
Sentinel Search
Probability Search
Ordered Search
5
-
8/3/2019 Lecture 3 (Stud Copy) - Searching
6/18
Sentinel Search
Algorithm sentinelsearch( val list val last val target )
1. list[last + 1] = target2. looker = 0
3. loop (target not equal list[looker] )looker = looker + 14 if ( looker
-
8/3/2019 Lecture 3 (Stud Copy) - Searching
7/18
Probability Search
Algorithm probabilitylsearch( val list val last val target )
looker = 0loop ( looker 0)
temp = list[looker 1]list[looker 1] = list[looker]list[looker] = templooker = looker - 1else
found = falsereturn foundend probabilitysearch
What is thedifferencebetween
probability searchand generalsequential search???
7
-
8/3/2019 Lecture 3 (Stud Copy) - Searching
8/18
Ordered SearchAlgorithm 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 = true4. else
found = false
return found
end of orderedsearch
What is thedifferencebetween orderedsearch andgeneral sequentialsearch ???
8
-
8/3/2019 Lecture 3 (Stud Copy) - Searching
9/18
Exercise:
Convert the sequential searchalgorithm into a program code.
You may use C++ or JAVA asthe programming language.
9
S l ti
-
8/3/2019 Lecture 3 (Stud Copy) - Searching
10/18
Solutionimport 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("Enter target value"));
if (seqsearch(list,last,target) )
JOptionPane.showMessageDialog(null, "The valuewas found in list ");
else
JOptionPane.showMessageDialog(null, "The valueis not in the list");
}
static boolean seqsearch(int list[], intlast, int target)
{int looker = 0;
boolean found;
while ( looker
-
8/3/2019 Lecture 3 (Stud Copy) - Searching
11/18
Binary Search
The sequential search algorithm is very slow. If wehave an array of 1000 elements, we must do 1000comparisons in the worst case. If the array is notsorted, the sequential search is the only solution. If
the array is sorted, we can use a more efficientalgorithm called the binary search.
The binary search starts by testing the data in the
element at the middle of the array to determine ifthe target is in the first or second half of thelist. If it is in the first half of the list, we do notneed to check the second half.
11
-
8/3/2019 Lecture 3 (Stud Copy) - Searching
12/18
Binary Search AlgorithmAlgorithm binarysearch ( val list
val end val target )
Search an ordered list using Binary Search
Pre list is ordered, it must contain at least one elementend is index to the largest element in the list
target is the value of element being sought
Post FOUND :Found set trueNOT FOUND :Found is set to false
Return found
12
-
8/3/2019 Lecture 3 (Stud Copy) - Searching
13/18
Binary Search Algorithm
first = 0
last = end
loop ( first list[mid] )first = mid + 1
3. else if ( target < list[mid] )
last = mid 1
4. elsefirst = 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
-
8/3/2019 Lecture 3 (Stud Copy) - Searching
14/18
ALGORITHM EFFICIENCY
Conclusion :
Which searching technique ismore efficient ????
14
-
8/3/2019 Lecture 3 (Stud Copy) - Searching
15/18
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 islinear, so its efficiency isO(n).
15
-
8/3/2019 Lecture 3 (Stud Copy) - Searching
16/18
Analyzing Search Algorithms
The binary search locates an item by repeatedlydividing the list in half. Its loop is
while ( first list[mid] )
first = mid + 1
else if ( target < list[mid])last = mid 1;
else
first = last + 1;
} 16
ALGORITHM EFFICIENCY
-
8/3/2019 Lecture 3 (Stud Copy) - Searching
17/18
ALGORITHM EFFICIENCY
This loops divides and it is thereforea logarithmic loop. So, the efficiency
is thus
O( log 2n)
17
-
8/3/2019 Lecture 3 (Stud Copy) - Searching
18/18
Exercise
Write a complete program that willimplement binary search to search anelement stored in an array.
You may write your program in C++ orJAVA code.
18