Title: Searching Algorithms
1Searching Algorithms
- Data Structures
- Rutgers University
2ReviewData Structures
- Collection Class Arrays, Linked Lists
- Advantages, Disadvantages
- Variations
- Stacks Queues
- Implementations
- Utility
- Variations (Priority queues?)
3Searching
- Computer systems often store large amounts of
data need to retrieve individual records based
on some search criterion - ? Fast searching is important
- ? What algorithms? What data structures?
- Sequential Searches
- Time is proportional to n ? time complexity O(n)
- Can use both arrays (unsorted) and linked lists
- Binary search
- Sorted array (What is the effort involved to
sort? We will address it later) - Time for search is proportional to log2 n
- Time complexity O(log n)
4Unordered List
- An unordered list is a linear collection of
entries whose relative positions with respect to
one another is irrelevant. - Unordered list is an unsorted list.
- Example
- Expense list with no particular order in which
items are listed. List entries contain Date,
Amount, item details. - Simple numbers
lt5, 43, 2, 9, 67, 5, 3, 2, 7gt
5Searching
How did we do it? What is the algorithm? Linked
lists Same? Array Same?
6Sequential Search
- A target is compared against every entry in the
list in sequence, one after the other. If the
target matches an entry, the search is
successful. If no match is found, the search is
unsuccessful. - Analysis Assume list has n elements
- Best case number of comparisons?
- Worst case number of comparisons?
- Number of comparisons for unsuccessful search?
- What is the average number of comparisons for
successful search? - Average depends on probability of what is being
searched?
7Average Case Analysis Sequential Search
- Average number of comparisons
- It is the number of comparisons required, on the
average, to search for any element in the
structure. - If there are n elements in the list, and each
element is searched for with the same likelihood
as any other - Ave comparisons (12n)/n n(n1)/2n
(n1)/2
8Average Case Analysis Sequential Search
- Non Uniform Distribution of Search Probabilities
Example -
Ave of comparisons 11/7 21/14 31/21
41/14 51/21 6 4/7 71/21 4.7
Note that if we assume equal probabilities for
each element (Pi 1/n), Then for sequential
search for unordered list, we get average
comparisons (n1)/2
9Sorting A teaser
RESULT ORDERED LIST How did we do it? What is
the algorithm? Linked lists Same? Array
Same? HOW TO DO EFFICIENTLY IS DISCUSSION FOR
FUTURE CLASS
10Ordered List
- An ordered list is a linear collection of entries
in which the entries are arranged in either
ascending order or descending order of keys - The key part of an entry that serves as the basis
of the ordering may vary depending on what kind
of entries are stored in the ordered list.
Example name, phone, city - Ordered List Advantage
Search can be faster ? Unordered list Search
time O(n)
11Ordered List Search time
- How do we search for a name in a phone book?
- Do we go through every entry in the phone book?
Searching in an ordered list is similar, and the
process is called binary search. Binary search in
a list of n entries, takes O(log n) time
Example Phone book with 1 million
entries Takes 1 second to compare two
names Sequential search worst case 1 million
seconds 11 days Binary search log(1000000)
sec 20 seconds
12Binary Search Algorithm
- If array size is zero print search failed
- Compare the target against the middle entry of
the array - If equal
- search is success
- else,
- decide to search on (left of middle entry) or
(right of middle entry) of the array (DIVIDE IN
HALF STEP) - Continue the search with new sub array
13Binary Search An Example
- Binary search for the key 19
14Binary Search Formal Algorithm
A array to be searched N number of elements in
the array T target being searched Variables
left and right are the array delimiters
- Algorithm binary_search (A,N,T)
- left ? 1
- right ? N
- while (left lt right) do
- mid ? (left right)/2
- if (T Amid) then
- break out of while loop
- else
- if (T lt Amid) then
- right ? (mid 1)
- else
- left ? (mid 1)
- endif
- endif
- endwhile
- if (left lt right) then
- display found at position, mid
15Binary Search Running Time Analysis
- Each step two comparisons, one to determine if
the target is equal to middle entry, the other
comparison is made to go left or right. - Thus two comparisons are made to go from a
sub-array to the next half sub-array - In the last step if success, it is only one
comparison, if failure two comparisons - The number of comparisons for a worst-case failed
search is always one more than the number of
comparisons for a worst-case successful search.
(Whereas it is same in sequential search case!)
16Worst-case analysis
?k log (N1) ?2 log(N1) 1 ? 2 log(N1)
- Assume of entries in array N 2k-1
- Worst case, need to go through k iterations
- 2k comparisons for worst-case failure
- 2k-1 comparisons for worst-case success
- If N is not in the form 2k-1
- Worst case,
- ? 2 log (N1) 1 for worst-case failure
- ? 2 log (N1) for worst-case success
- y x means that y is the smaller integer
greater than or equal to x example 2.3 3 - Worst case, O(log n) Average can be different
17Notes on Binary Search
- Binary search of the order O(log n) is possible
on array - But not on a linked list
- Every step in binary search accesses the middle
entry of the list - In an array this is a direct access that takes
O(1) time - In linked list of length n, accessing middle
entry is O(n) time - Therefore the entire algorithm with linked list
would be greater than O(log n).
18Performance Comparison
Arrays Simple, fast Inflexible O(1) unordered
list O(n) ordered list O(n) unordered list
ordered list O(n) unordered list O(logn) bin
search ordered list
Add Delete Find
Linked List Simple Flexible O(1) ordered -gt no
adv O(1) - any O(n) - specific O(n) (no bin
search)
Trees Still Simple Flexible ? ? O(log n)
- NEXT CLASS
- Recursion
- Binary Trees