Title: ECE 242 Spring 2003 Data Structures in Java
1ECE 242 Spring 2003Data Structures in Java
- http//rio.ecs.umass.edu/ece242
- Search
- Prof. Lixin Gao
2Todays Topics
- Applications
- Sequential Search
- Binary Search
- Implemented by while loop
- Implemented by recursion
3Applications
- Search is used everywhere in our daily life.
- Search telephone number in telephone book
- Search grade for Project 3 on ECE242 website
- Search course schedule on registration guide
- Search flight schedule on airline
- Anything else?
- Search is performed the most often in database
4Search
6
21
A set of records are stored in array, linked
list, or others. Given a target key, find the
record(s) that have the same key.
5
13
target
4
?
8
3
8
5
2
3
1
2
A
head
B
C
1
2
3
B
target
5Key
Find the student whose ID 8
ID
NAME
SCORE
6
5
4
?
3
2
target
1
8
The key is the field in your record that is used
for searching. In the above example, student ID
is the key.
6Search Algorithm
- A step-by-step description of how to solve the
search problem - Some search algorithms
- sequential search
- binary search
7Sequential Search
- A simple search algorithm
- compare the target key to the key of records one
by one starting from the first record - Easy to implement for Array and Linked List
8Sequential Search For Array
- An Array db which stores integer numbers
- Searching 8 in this array
- If found, return index
- Otherwise, return 1
9Pseudo-code of Sequential Search For Array
- int SequentialSearch( int key, int db )
- int index
- for( index0 indexltdb.length index )
- if( dbindexkey )
- return index
-
- return 1
-
6
21
5
13
target
4
?
8
3
8
5
2
3
1
2
10Sequential Search For Linked List
- Sequential Search also works for Linked List
- An Linked List db, in which each node stores a
string - Searching B in this list
- If found, return that node
- Otherwise, return null
11Pseudo-code of Sequential Searching For Linked
List
- DNode SequentialSearch( String key, SLList db )
- DNode probe db.head
- while ( probe!null )
- if( probe.value.equals(key) )
- return probe
- probe probe.next
-
- return null
-
B
target
A
head
B
C
1
2
3
12Complexity of Sequential Search
- Suppose there are n records
- In the best case, the target is the first entry.
It only takes 1 step - In the worst case, the target is the last entry,
or it cannot be found. Sequential search has to
scan all n records and takes totally n steps. - Cost O(n)
13Binary Search
- If the records are sorted by the key, we can do
better. - Think about how you look up a word in English
dictionary where the words are sorted
alphabetically. - Binary Search. (we mentioned it before)
14Binary Search
- If the target is present in the list, it must be
one between low and high
A
B
C
M
X
Y
Z
middle12
high25
low0
Take a look at the middle one. middle
(lowhigh) / 2
15Binary Search Case 1
A
B
C
X
Y
Z
M
middle
high25
low0
If the target is the same as the key value in the
middle position, we have found the record.
Return the value of middle
16Binary Search Case 2
A
B
C
X
Y
Z
M
L
high11
low0
If target lt the key value in the middle position,
we know that it can not be found in the right
half. We only need to search in the left half.
high middle - 1
17Binary Search Case 3
A
B
C
X
Y
Z
N
M
high25
low13
If target gt the key value in the middle position,
we know that it can not be found in the left
half. We only need to search the right half.
low middle 1
18Pseudo-code of Binary Search
- int BinarySearch(char target, char array)
int low0, high array.length-1,
middle while( lowlthigh) middle
(lowhigh)/2 if( arraymiddletarget
) return middle else if(
arraymiddlelttarget ) low middle
1 else high middle -
1 return -1
19Sample Trace For Searching H
- Examples BinarySearchWhileLoop.java
- The Array is
- A B C D E F G H I J K L M N O P Q R S T
- Give me a search key H
- low0 high19 middle9 arraymiddleJ
- low0 high8 middle4 arraymiddleE
- low5 high8 middle6 arraymiddleG
- low7 high8 middle7 arraymiddleH
- Found at position 7
20Recursive Binary Search
- Main ideas compare the key value to the middle
value - equals, done
- less than, search the left half of the list only
- greater than, search the right half of the list
only
M
Left half
Right half
21Code For Recursive Binary Search
- static int BinarySearch(char target, int low, int
high, char array) - if( targetltarraylow targetgtarrayhigh )
- return -1
- int middle (lowhigh)/2
- if( arraymiddletarget )
- return middle
- else if( arraymiddlegttarget )
- return BinarySearch(target, low,
middle-1, array) - else
- return BinarySearch(target,
middle1, high, array) -
22Sample Trace For Searching H
- Examples BinarySearchRecursive.java
- The Array is
- A B C D E F G H I J K L M N O P Q R S T
- Give me a search key H
- low0 high19 middle9 arraymiddleJ
- low0 high8 middle4 arraymiddleE
- low5 high8 middle6 arraymiddleG
- low7 high8 middle7 arraymiddleH
- Found at position 7
23Complexity For Binary Search
- Suppose there are n records
- In the best case, the target is the middle entry.
It only takes 1 step - In the worst case, it will take log2n 1 steps.
- Cost O(logN)
24What Happens In Linked List?
- Binary Search is easy to implement in Array
- What happens when searching in Linked List?
- Locate an element in linked list
- How can you get the middle node directly?
- Can not index linked list
head
A
B
C
D
E
middle node
25Motivation for Binary Tree
- In linked list, it is hard to implement binary
search - We need an index to each node
- Next class, we will talk about Binary Tree and
Binary Search Tree
index
index
index
index
index
A
B
C
D
E