Java Data Structures & Algorithms Guide
Java Data Structures & Algorithms Guide
Table of Content
• AIM
* Classification of Sorting Methods,
* Bubble Sort,
* Selection Sort,
* Insertion Sort,
* Quick Sort,
* Merge Sort,
* Heap Sort
* Radix Sort,
* Classification of Searching,
* Linear Search, Binary Search,
* Hashing Functions – Division Reminder, Mid Square Method, Folding Method;
* Collision Resolution Techniques.
* String search and matching.
CHAPTER 4
77 42 35 12 101 5
1 2 3 4 5 6
5 12 35 42 77 101
Selection Sort
5 1 3 4 6 2
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 6 2
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 6 2
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 6 2
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 6 2
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 6 2
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 6 2
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 6 2
Largest
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 2 6
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 2 6
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 2 6
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 2 6
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 2 6
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 2 6
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 2 6
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 2 6
Largest
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Largest
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Largest
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Largest
Comparison
Data Movement
Sorted
Selection Sort
1 2 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
1 2 3 4 5 6
DONE!
Comparison
Data Movement
Sorted
Bubble Sort
"Bubbling Up" the Largest
Element
• Traverse a collection of elements
• Move from the front to the end
• “Bubble” the largest value to the end using pair-wise
comparisons and swapping
1 2 3 4 5 6
77 42 35 12 101 5
"Bubbling Up" the Largest
Element
• Traverse a collection of elements
• Move from the front to the end
• “Bubble” the largest value to the end using pair-wise
comparisons and swapping
1 2 3 4 5 6
42 Swap42
77 77 35 12 101 5
"Bubbling Up" the Largest
Element
• Traverse a collection of elements
• Move from the front to the end
• “Bubble” the largest value to the end using pair-wise
comparisons and swapping
1 2 3 4 5 6
42 7735 Swap35
77 12 101 5
"Bubbling Up" the Largest
Element
• Traverse a collection of elements
• Move from the front to the end
• “Bubble” the largest value to the end using pair-wise
comparisons and swapping
1 2 3 4 5 6
42 35 12 Swap12
77 77 101 5
"Bubbling Up" the Largest
Element
• Traverse a collection of elements
• Move from the front to the end
• “Bubble” the largest value to the end using pair-wise
comparisons and swapping
1 2 3 4 5 6
42 35 12 77 101 5
No need to swap
"Bubbling Up" the Largest
Element
• Traverse a collection of elements
• Move from the front to the end
• “Bubble” the largest value to the end using pair-wise
comparisons and swapping
1 2 3 4 5 6
42 35 12 77 5 Swap 101
101 5
"Bubbling Up" the Largest
Element
• Traverse a collection of elements
• Move from the front to the end
• “Bubble” the largest value to the end using pair-wise
comparisons and swapping
1 2 3 4 5 6
42 35 12 77 5 101
loop
exitif(index > last_index)
if(A[index] > A[index + 1]) then
Swap(A[index], A[index + 1])
endif
index = index + 1
endloop
LB
1 2 3 4 5 6
42 35 12 77 5 101
1 2 3 4 5 6
35 12 42 5 77 101
1 2 3 4 5 6
N-1
12 35 5 42 77 101
1 2 3 4 5 6
12 5 35 42 77 101
1 2 3 4 5 6
5 12 35 42 77 101
Reducing the Number of
Comparisons
1 2 3 4 5 6
77 42 35 12 101 5
1 2 3 4 5 6
42 35 12 77 5 101
1 2 3 4 5 6
35 12 42 5 77 101
1 2 3 4 5 6
12 35 5 42 77 101
1 2 3 4 5 6
12 5 35 42 77 101
Reducing the Number
of Comparisons
• On the Nth “bubble up”, we only
need to
do MAX-N comparisons.
• For example:
• This is the 4th “bubble up”
• MAX is 6
•1 Thus
2
we have
3
2 comparisons
4 5 6
to do
12 35 5 42 77 101
Putting It All
Together
N is … // Size of Array
loop
exitif(to_do == 0)
index = 0
loop
exitif(index > to_do)
Outer loop
Inner loop
if(A[index] > A[index + 1]) then
Swap(A[index], A[index + 1])
endif
index = index + 1
endloop
to_do = to_do - 1
endloop
endprocedure // Bubblesort
Already Sorted Collections?
• What if the collection was already
sorted?
• What if only a few elements were
out of place and after a couple of
“bubble ups,” the collection was
sorted?
1 2 3 4 5 6
• We want to be able to detect this
42 77 101
and5 “stop
12 35
early”!
Using a Boolean “Flag”
• We can use a boolean variable to determine if any
swapping occurred during the “bubble up.”
loop
exitif ((to_do == 0) OR NOT(did_swap))
index = 0
did_swap = false
loop
exitif(index > to_do)
if(A[index] > A[index + 1]) then
Swap(A[index], A[index + 1])
did_swap = true
endif
index = index + 1
endloop
to_do = to_do - 1
endloop
An Animated Example
N 8 did_swap true
to_do 7
index
98 23 45 14 6 67 33 42
1 2 3 4 5 6 7 8
An Animated Example
N 8 did_swap false
to_do 7
index 1
98 23 45 14 6 67 33 42
1 2 3 4 5 6 7 8
An Animated Example
N 8 did_swap false
to_do 7
index 1
Swap
98 23 45 14 6 67 33 42
1 2 3 4 5 6 7 8
An Animated Example
N 8 did_swap true
to_do 7
index 1
Swap
23 98 45 14 6 67 33 42
1 2 3 4 5 6 7 8
An Animated Example
N 8 did_swap true
to_do 7
index 2
23 98 45 14 6 67 33 42
1 2 3 4 5 6 7 8
An Animated Example
N 8 did_swap true
to_do 7
index 2
Swap
23 98 45 14 6 67 33 42
1 2 3 4 5 6 7 8
An Animated Example
N 8 did_swap true
to_do 7
index 2
Swap
23 45 98 14 6 67 33 42
1 2 3 4 5 6 7 8
An Animated Example
N 8 did_swap true
to_do 7
index 3
23 45 98 14 6 67 33 42
1 2 3 4 5 6 7 8
An Animated Example
N 8 did_swap true
to_do 7
index 3
Swap
23 45 98 14 6 67 33 42
1 2 3 4 5 6 7 8
An Animated Example
N 8 did_swap true
to_do 7
index 3
Swap
23 45 14 98 6 67 33 42
1 2 3 4 5 6 7 8
An Animated Example
N 8 did_swap true
to_do 7
index 4
23 45 14 98 6 67 33 42
1 2 3 4 5 6 7 8
An Animated Example
N 8 did_swap true
to_do 7
index 4
Swap
23 45 14 98 6 67 33 42
1 2 3 4 5 6 7 8
An Animated Example
N 8 did_swap true
to_do 7
index 4
Swap
23 45 14 6 98 67 33 42
1 2 3 4 5 6 7 8
An Animated Example
N 8 did_swap true
to_do 7
index 5
23 45 14 6 98 67 33 42
1 2 3 4 5 6 7 8
An Animated Example
N 8 did_swap true
to_do 7
index 5
Swap
23 45 14 6 98 67 33 42
1 2 3 4 5 6 7 8
An Animated Example
N 8 did_swap true
to_do 7
index 5
Swap
23 45 14 6 67 98 33 42
1 2 3 4 5 6 7 8
An Animated Example
N 8 did_swap true
to_do 7
index 6
23 45 14 6 67 98 33 42
1 2 3 4 5 6 7 8
An Animated Example
N 8 did_swap true
to_do 7
index 6
Swap
23 45 14 6 67 98 33 42
1 2 3 4 5 6 7 8
An Animated Example
N 8 did_swap true
to_do 7
index 6
Swap
23 45 14 6 67 33 98 42
1 2 3 4 5 6 7 8
An Animated Example
N 8 did_swap true
to_do 7
index 7
23 45 14 6 67 33 98 42
1 2 3 4 5 6 7 8
An Animated Example
N 8 did_swap true
to_do 7
index 7
Swap
23 45 14 6 67 33 98 42
1 2 3 4 5 6 7 8
An Animated Example
N 8 did_swap true
to_do 7
index 7
Swap
23 45 14 6 67 33 42 98
1 2 3 4 5 6 7 8
After First Pass of Outer Loop
N 8 did_swap true
to_do 7
23 45 14 6 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”
N 8 did_swap false
to_do 6
index 1
23 45 14 6 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”
N 8 did_swap false
to_do 6
index 1
No Swap
23 45 14 6 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”
N 8 did_swap false
to_do 6
index 2
23 45 14 6 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”
N 8 did_swap false
to_do 6
index 2
Swap
23 45 14 6 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”
N 8 did_swap true
to_do 6
index 2
Swap
23 14 45 6 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”
N 8 did_swap true
to_do 6
index 3
23 14 45 6 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”
N 8 did_swap true
to_do 6
index 3
Swap
23 14 45 6 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”
N 8 did_swap true
to_do 6
index 3
Swap
23 14 6 45 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”
N 8 did_swap true
to_do 6
index 4
23 14 6 45 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”
N 8 did_swap true
to_do 6
index 4
No Swap
23 14 6 45 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”
N 8 did_swap true
to_do 6
index 5
23 14 6 45 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”
N 8 did_swap true
to_do 6
index 5
Swap
23 14 6 45 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”
N 8 did_swap true
to_do 6
index 5
Swap
23 14 6 45 33 67 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”
N 8 did_swap true
to_do 6
index 6
23 14 6 45 33 67 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”
N 8 did_swap true
to_do 6
index 6
Swap
23 14 6 45 33 67 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”
N 8 did_swap true
to_do 6
index 6
Swap
23 14 6 45 33 42 67 98
1 2 3 4 5 6 7 8
After Second Pass of Outer
Loop
N 8 did_swap true
to_do 6
23 14 6 45 33 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”
N 8 did_swap false
to_do 5
index 1
23 14 6 45 33 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”
N 8 did_swap false
to_do 5
index 1
Swap
23 14 6 45 33 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”
N 8 did_swap true
to_do 5
index 1
Swap
14 23 6 45 33 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”
N 8 did_swap true
to_do 5
index 2
14 23 6 45 33 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”
N 8 did_swap true
to_do 5
index 2
Swap
14 23 6 45 33 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”
N 8 did_swap true
to_do 5
index 2
Swap
14 6 23 45 33 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”
N 8 did_swap true
to_do 5
index 3
14 6 23 45 33 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”
N 8 did_swap true
to_do 5
index 3
No Swap
14 6 23 45 33 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”
N 8 did_swap true
to_do 5
index 4
14 6 23 45 33 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”
N 8 did_swap true
to_do 5
index 4
Swap
14 6 23 45 33 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”
N 8 did_swap true
to_do 5
index 4
Swap
14 6 23 33 45 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”
N 8 did_swap true
to_do 5
index 5
14 6 23 33 45 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”
N 8 did_swap true
to_do 5
index 5
Swap
14 6 23 33 45 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”
N 8 did_swap true
to_do 5
index 5
Swap
14 6 23 33 42 45 67 98
1 2 3 4 5 6 7 8
After Third Pass of Outer
Loop
N 8 did_swap true
to_do 5
14 6 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fourth “Bubble Up”
N 8 did_swap false
to_do 4
index 1
14 6 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fourth “Bubble Up”
N 8 did_swap false
to_do 4
index 1
Swap
14 6 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fourth “Bubble Up”
N 8 did_swap true
to_do 4
index 1
Swap
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fourth “Bubble Up”
N 8 did_swap true
to_do 4
index 2
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fourth “Bubble Up”
N 8 did_swap true
to_do 4
index 2
No Swap
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fourth “Bubble Up”
N 8 did_swap true
to_do 4
index 3
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fourth “Bubble Up”
N 8 did_swap true
to_do 4
index 3
No Swap
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fourth “Bubble Up”
N 8 did_swap true
to_do 4
index 4
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fourth “Bubble Up”
N 8 did_swap true
to_do 4
index 4
No Swap
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
After Fourth Pass of Outer
Loop
N 8 did_swap true
to_do 4
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fifth “Bubble Up”
N 8 did_swap false
to_do 3
index 1
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fifth “Bubble Up”
N 8 did_swap false
to_do 3
index 1
No Swap
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fifth “Bubble Up”
N 8 did_swap false
to_do 3
index 2
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fifth “Bubble Up”
N 8 did_swap false
to_do 3
index 2
No Swap
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fifth “Bubble Up”
N 8 did_swap false
to_do 3
index 3
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fifth “Bubble Up”
N 8 did_swap false
to_do 3
index 3
No Swap
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
After Fifth Pass of Outer Loop
N 8 did_swap false
to_do 3
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
Finished “Early”
N 8 did_swap false
to_do 3
We didn’t do any swapping,
index 4 so all of the other elements
must be correctly placed.
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
Summary
• “Bubble Up” algorithm will move
largest value to its correct location
(to the right)
• Repeat “Bubble Up” until all
elements are correctly placed:
• Maximum of N-1 times
• Can finish early if no swapping
occurs
• We reduce the number of elements
we compare each time one is
correctly placed
LB
Truth in CS Act
• NOBODY EVER USES BUBBLE SORT
• NOBODY
• NOT EVER
• Start with an empty left hand and the cards face down
on the table.
• Then remove one card at a time from the table, and
insert it into the correct position in the left hand.
• To find the correct position for a card, compare it with
each of the cards already in the hand, from right to left.
• At all times, the cards held in the left hand are sorted,
and these cards were originally the top cards of the pile
on the table.
Insertion sort (Example)
Insertion sort (Example)
Insertion sort
(Algorithm)
Divide and Conquer strategy
(Merge Sort)
Overview
• Learn the technique of “divide and
conquer”
in the context of merge sort.
A Sorting Problem
(Divide and Conquer Approach)
• Divide the problem into a number of sub
problems.
• Conquer the sub problems by solving
them recursively.
• Base case: If the sub problems are small
enough, just solve them by brute force.
• Combine the sub problem solutions to
give a solution to the original problem.
Merge sort
• A sorting algorithm based on divide and conquer. Its worst-
case running time has a lower order of growth than insertion
sort.
• Because we are dealing with sub problems, we state each
sub problem as sorting a sub array A[p . . r ].
• Initially, p = 1 and r = n, but these values change as we
recurse through sub problems.
To sort A[p . . r ]:
• Divide by splitting into two sub arrays A[p . . q] and A[q + 1
. . r ], where q is the halfway point of A[p . . r ].
• Conquer by recursively sorting the two sub arrays A[p . . q]
and A[q + 1 . . r ].
• Combine by merging the two sorted sub arrays A[p . . q]
and A[q + 1 . . r ] to produce a single sorted sub array
A[p . . r ]. To accomplish this step, we’ll define a procedure
MERGE(A, p, q, r ).
Merge Sort (Algorithm)
The recursion bottoms out when the subarray has
just 1 element, so that it’s trivially sorted.
Merging
Input: Array A and indices p, q, r such that
• p≤q<r.
• Subarray A[p . . q] is sorted and subarray A[q + 1 . .
r ] is sorted. By the restrictions on p, q, r , neither
subarray is empty.
Administrator
Display Retrieve User Info
Account
User Profile
Info
Validate
Update
User Info
Enter/Update/ Delete
Update/Delete User Info
User Info
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
Access User Info
Administrator
Display Retrieve User Info
Account
User Profile
Info
Validate
Update
User Info
Enter/Update/ Delete
Update/Delete User Info
User Info
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
Access User Info
98 23 45 14
Administrator
Display Retrieve User Info
Account
User Profile
Info
Validate
Update
User Info
Enter/Update/ Delete
Update/Delete User Info
User Info
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
Access User Info
98 23 45 14
Administrator
98 23 Display Retrieve User Info
Account
User Profile
Info
Validate
Update
User Info
Enter/Update/ Delete
Update/Delete User Info
User Info
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
Access User Info
98 23 45 14
Administrator
98 23 Display Retrieve User Info
Account
User Profile
Info
Validate
Update
User Info
Enter/Update/ Delete
Update/Delete User Info
User Info
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
Access User Info
98 23 45 14
Administrator
98 23 Display Retrieve User Info
Account
User Profile
Info
23
User Account Info
Merge
Validate
Update
User Info
Enter/Update/ Delete
Update/Delete User Info
User Info
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
Access User Info
98 23 45 14
Administrator
98 23 Display Retrieve User Info
Account
User Profile
Info
23 98
User Account Info
Merge
Validate
Update
User Info
Enter/Update/ Delete
Update/Delete User Info
User Info
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
Access User Info
98 23 45 14
Administrator
98 23 45 14
Display Retrieve User Info
Account
User Profile
Info
23 98
User Account Info
Validate
Update
User Info
Enter/Update/ Delete
Update/Delete User Info
User Info
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
Access User Info
98 23 45 14
Administrator
98 23 45 14
Display Retrieve User Info
Account
User Profile
Info
23 98
User Account Info
Merge
Validate
Update
User Info
Enter/Update/ Delete
Update/Delete User Info
User Info
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
Access User Info
98 23 45 14
Administrator
98 23 45 14
Display Retrieve User Info
Account
User Profile
Info
23 98 14
User Account Info
Merge
Validate
Update
User Info
Enter/Update/ Delete
Update/Delete User Info
User Info
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
Access User Info
98 23 45 14
Administrator
98 23 45 14
Display Retrieve User Info
Account
User Profile
Info
23 98 14 45
User Account Info
Merge
Validate
Update
User Info
Enter/Update/ Delete
Update/Delete User Info
User Info
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
Access User Info
98 23 45 14
Administrator
98 23 45 14
Display Retrieve User Info
Account
User Profile
Info
23 98 14 45
User Account Info
Validate
Update
Merge User Info
Enter/Update/ Delete
Update/Delete User Info
User Info
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
Access User Info
98 23 45 14
Administrator
98 23 45 14
Display Retrieve User Info
Account
User Profile
Info
23 98 14 45
User Account Info
14
Validate
Update
Merge User Info
Enter/Update/ Delete
Update/Delete User Info
User Info
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
Access User Info
98 23 45 14
Administrator
98 23 45 14
Display Retrieve User Info
Account
User Profile
Info
23 98 14 45
User Account Info
14 23
Validate
Update
Merge User Info
Enter/Update/ Delete
Update/Delete User Info
User Info
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
Access User Info
98 23 45 14
Administrator
98 23 45 14
Display Retrieve User Info
Account
User Profile
Info
23 98 14 45
User Account Info
14 23 45
Validate
Update
Merge User Info
Enter/Update/ Delete
Update/Delete User Info
User Info
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
Access User Info
98 23 45 14
Administrator
98 23 45 14
Display Retrieve User Info
Account
User Profile
Info
23 98 14 45
User Account Info
14 23 45 98
Validate
Update
Merge User Info
Enter/Update/ Delete
Update/Delete User Info
User Info
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
Access User Info
98 23 45 14 6 67 33 42
Administrator
98 23 45 14
Display Retrieve User Info
Account
User Profile
Info
23 98 14 45
User Account Info
14 23 45 98
Validate
Update
User Info
Enter/Update/ Delete
Update/Delete User Info
User Info
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
Access User Info
98 23 45 14 6 67 33 42
Administrator
98 23 45 14
Display 6 Retrieve
67 User Info
Account
User Profile
Info
23 98 14 45
User Account Info
14 23 45 98
Validate
Update
User Info
Enter/Update/ Delete
Update/Delete User Info
User Info
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
Access User Info
98 23 45 14 6 67 33 42
Administrator
98 23 45 14
Display 6 Retrieve
67 User Info
Account
User Profile
Info
23 98 14 45
User Account Info
14 23 45 98 Merge
Validate
Update
User Info
Enter/Update/ Delete
Update/Delete User Info
User Info
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
Access User Info
98 23 45 14 6 67 33 42
Administrator
98 23 45 14
Display 6 Retrieve
67 User Info
Account
User Profile
Info
23 98 14 45 6
User Account Info
14 23 45 98 Merge
Validate
Update
User Info
Enter/Update/ Delete
Update/Delete User Info
User Info
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
Access User Info
98 23 45 14 6 67 33 42
Administrator
98 23 45 14
Display 6 Retrieve
67 User Info
Account
User Profile
Info
23 98 14 45 6 67
User Account Info
14 23 45 98 Merge
Validate
Update
User Info
Enter/Update/ Delete
Update/Delete User Info
User Info
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
Access User Info
98 23 45 14 6 67 33 42
Administrator
98 23 45 14
Display 6 Retrieve
67 User
33 Info 42
Account
User Profile
Info
23 98 14 45 6 67
User Account Info
14 23 45 98
Validate
Update
User Info
Enter/Update/ Delete
Update/Delete User Info
User Info
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
Access User Info
98 23 45 14 6 67 33 42
Administrator
98 23 45 14
Display 6 Retrieve
67 User
33 Info 42
Account
User Profile
Info
23 98 14 45 6 67
User Account Info
14 23 45 98 Merge
Validate
Update
User Info
Enter/Update/ Delete
Update/Delete User Info
User Info
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
Access User Info
98 23 45 14 6 67 33 42
Administrator
98 23 45 14
Display 6 Retrieve
67 User
33 Info 42
Account
User Profile
Info
23 98 14 45 6 67 33
User Account Info
14 23 45 98 Merge
Validate
Update
User Info
Enter/Update/ Delete
Update/Delete User Info
User Info
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
Access User Info
98 23 45 14 6 67 33 42
Administrator
98 23 45 14
Display 6 Retrieve
67 User
33 Info 42
Account
User Profile
Info
23 98 14 45 6 67 33 42
User Account Info
14 23 45 98 Merge
Validate
Update
User Info
Enter/Update/ Delete
Update/Delete User Info
User Info
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
Access User Info
98 23 45 14 6 67 33 42
Administrator
98 23 45 14
Display 6 Retrieve
67 User
33 Info 42
Account
User Profile
Info
23 98 14 45 6 67 33 42
User Account Info
14 23 45 98
Validate
Update
User Info
Merge
Enter/Update/ Delete
Update/Delete User Info
User Info
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
Access User Info
98 23 45 14 6 67 33 42
Administrator
98 23 45 14
Display 6 Retrieve
67 User
33 Info 42
Account
User Profile
Info
23 98 14 45 6 67 33 42
User Account Info
14 23 45 98 6
Validate
Update
User Info
Merge
Enter/Update/ Delete
Update/Delete User Info
User Info
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
Access User Info
98 23 45 14 6 67 33 42
Administrator
98 23 45 14
Display 6 Retrieve
67 User
33 Info 42
Account
User Profile
Info
23 98 14 45 6 67 33 42
User Account Info
14 23 45 98 6 33
Validate
Update
User Info
Merge
Enter/Update/ Delete
Update/Delete User Info
User Info
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
Access User Info
98 23 45 14 6 67 33 42
Administrator
98 23 45 14
Display 6 Retrieve
67 User
33 Info 42
Account
User Profile
Info
23 98 14 45 6 67 33 42
User Account Info
14 23 45 98 6 33 42
Validate
Update
User Info
Merge
Enter/Update/ Delete
Update/Delete User Info
User Info
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
Access User Info
98 23 45 14 6 67 33 42
Administrator
98 23 45 14
Display 6 Retrieve
67 User
33 Info 42
Account
User Profile
Info
23 98 14 45 6 67 33 42
User Account Info
14 23 45 98 6 33 42 67
Validate
Update
User Info
Merge
Enter/Update/ Delete
Update/Delete User Info
User Info
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
Access User Info
98 23 45 14 6 67 33 42
Administrator
98 23 45 14
Display 6 Retrieve
67 User
33 Info 42
Account
User Profile
Info
23 98 14 45 6 67 33 42
User Account Info
14 23 45 98 6 33 42 67
Validate
Update
User Info
Enter/Update/ Delete
Update/Delete User Info
User Info
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
Access User Info
98 23 45 14 6 67 33 42
Administrator
98 23 45 14
Display 6 Retrieve
67 User
33 Info 42
Account
User Profile
Info
23 98 14 45 6 67 33 42
User Account Info
14 23 45 98 6 33 42 67
Validate
Update
6
Enter/Update/ Delete User Info
Update/Delete User Info
User Info
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
Access User Info
98 23 45 14 6 67 33 42
Administrator
98 23 45 14
Display 6 Retrieve
67 User
33 Info 42
Account
User Profile
Info
23 98 14 45 6 67 33 42
User Account Info
14 23 45 98 6 33 42 67
Validate
Update
6
Enter/Update/ Delete 14 User Info
Update/Delete User Info
User Info
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
Access User Info
98 23 45 14 6 67 33 42
Administrator
98 23 45 14
Display 6 Retrieve
67 User
33 Info 42
Account
User Profile
Info
23 98 14 45 6 67 33 42
User Account Info
14 23 45 98 6 33 42 67
Validate
Update
6
Enter/Update/ Delete 14 23
User Info
Update/Delete User Info
User Info
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
Access User Info
98 23 45 14 6 67 33 42
Administrator
98 23 45 14
Display 6 Retrieve
67 User
33 Info 42
Account
User Profile
Info
23 98 14 45 6 67 33 42
User Account Info
14 23 45 98 6 33 42 67
Validate
Update
6
Enter/Update/ Delete 14 23 33
User Info
Update/Delete User Info
User Info
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
Access User Info
98 23 45 14 6 67 33 42
Administrator
98 23 45 14
Display 6 Retrieve
67 User
33 Info 42
Account
User Profile
Info
23 98 14 45 6 67 33 42
User Account Info
14 23 45 98 6 33 42 67
Validate
Update
6
Enter/Update/ Delete 14 23 33
User Info 42
Update/Delete User Info
User Info
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
Access User Info
98 23 45 14 6 67 33 42
Administrator
98 23 45 14
Display 6 Retrieve
67 User
33 Info 42
Account
User Profile
Info
23 98 14 45 6 67 33 42
User Account Info
14 23 45 98 6 33 42 67
Validate
Update
6
Enter/Update/ Delete 14 23 33
User Info 42 45
Update/Delete User Info
User Info
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
Access User Info
98 23 45 14 6 67 33 42
Administrator
98 23 45 14
Display 6 Retrieve
67 User
33 Info 42
Account
User Profile
Info
23 98 14 45 6 67 33 42
User Account Info
14 23 45 98 6 33 42 67
Validate
Update
6
Enter/Update/ Delete 14 23 33
User Info 42 45 67
Update/Delete User Info
User Info
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
Access User Info
98 23 45 14 6 67 33 42
Administrator
98 23 45 14
Display 6 Retrieve
67 User
33 Info 42
Account
User Profile
Info
23 98 14 45 6 67 33 42
User Account Info
14 23 45 98 6 33 42 67
Validate
Update
6
Enter/Update/ Delete 14 23 33
User Info 42 45 67 98
Update/Delete User Info
User Info
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
Access User Info
98 23 45 14 6 67 33 42
Administrator
98 23 45 14
Display 6 Retrieve
67 User
33 Info 42
Account
User Profile
Info
23 98 14 45 6 67 33 42
User Account Info
14 23 45 98 6 33 42 67
Validate
Update
6
Enter/Update/ Delete 14 23 33
User Info 42 45 67 98
Update/Delete User Info
User Info
98 23 45 14 6 67 33 42
Administrator
Display Retrieve User Info
Account
User Profile
Info
Validate
Update
6
Enter/Update/ Delete 14 23 33
User Info 42 45 67 98
Update/Delete User Info
User Info
Example [A call of MERGE(9, 12, 16)]
Analyzing divide-and-conquer
algorithms
Analyzing merge sort
Recursion tree (Step 1)
Recursion tree (Step 2)
Recursion tree (Step n)
Divide and Conquer strategy
(Quick Sort)
Overview
• Sorts in place.
Description of
quicksort
Performance of quicksort
The running time of quicksort depends on the
partitioning of the subarrays:
• If the subarrays are balanced, then
quicksort can run as fast as mergesort.
• If they are unbalanced, then quicksort can run
as slowly as insertion sort.
Randomized version of quicksort
• We have assumed that all input permutations are equally
likely.
• This is not always true.
• To correct this, we add randomization to quicksort.
• We could randomly permute the input array.
• Instead, we use random sampling, or picking one
element at random.
• Don’t always use A[r ] as the pivot. Instead, randomly
pick an element from the subarray that is being sorted.
• Findforthe
j=1 tofrequencies
A. length of each object and store
it in CC[ array.
A[j] ] = C[ A[j] ] + 1;
0 1 2 3 4 5
C 2 0 2 3 0 1
Counting Sort
• Let us illustrate the counting sort with an
example.
Apply the concept of counting sort on the given
1 2 3 4 5 6 7 8
A 2 5 array.
3 0 2 3 0 3
• Findforthe
j=1 tofrequencies
A. length of each object and store
0 1 2 3 4 5
it in C array.
C[ A[j] ] = C[ A[j] ] + 1; C 2 0 2 3 0 1
0 1 2 3 4 5
• And then cumulatively add CC array.
2 2 4 7 7 8
for i=1 to k
C[i] = C[i] + C[i-1];
Counting Sort
for j=A. length down to 1
B[C[ A[j] ]] = A[j]; 1 2 3 4 5 6 7 8
C[ A[j] ] = C[ A[j] ] - 1; A 2 5 3 0 2 3 0 3
j
1 2 3 4 5 6 7 8 0 1 2 3 4 5
B 3 C 2 2 4 7 7 8
Counting Sort
for j=A. length down to 1
B[C[ A[j] ]] = A[j]; 1 2 3 4 5 6 7 8
C[ A[j] ] = C[ A[j] ] - 1; A 2 5 3 0 2 3 0 3
j
1 2 3 4 5 6 7 8 0 1 2 3 4 5
B 3 C 2 2 4 7 7 8
1 2 3 4 5 6 7 8
A 2 5 3 0 2 3 0 3
j
1 2 3 4 5 6 7 8 0 1 2 3 4 5
B 3 C 2 2 4 6 7 8
Counting Sort
for j=A. length down to 1
B[C[ A[j] ]] = A[j]; 1 2 3 4 5 6 7 8
C[ A[j] ] = C[ A[j] ] - 1; A 2 5 3 0 2 3 0 3
j
1 2 3 4 5 6 7 8 0 1 2 3 4 5
B 0 3 C 2 2 4 6 7 8
Counting Sort
for j=A. length down to 1
B[C[ A[j] ]] = A[j]; 1 2 3 4 5 6 7 8
C[ A[j] ] = C[ A[j] ] - 1; A 2 5 3 0 2 3 0 3
j
1 2 3 4 5 6 7 8 0 1 2 3 4 5
B 0 3 C 2 2 4 6 7 8
1 2 3 4 5 6 7 8
A 2 5 3 0 2 3 0 3
j
1 2 3 4 5 6 7 8 0 1 2 3 4 5
B 0 3 C 1 2 4 6 7 8
Counting Sort
for j=A. length down to 1
B[C[ A[j] ]] = A[j]; 1 2 3 4 5 6 7 8
C[ A[j] ] = C[ A[j] ] - 1; A 2 5 3 0 2 3 0 3
j
1 2 3 4 5 6 7 8 0 1 2 3 4 5
B 0 3 3 C 1 2 4 6 7 8
Counting Sort
for j=A. length down to 1
B[C[ A[j] ]] = A[j];
1 2 3 4 5 6 7 8
C[ A[j] ] = C[ A[j] ] - 1;
A 2 5 3 0 2 3 0 3
j
1 2 3 4 5 6 7 8 0 1 2 3 4 5
B 0 3 3 C 1 2 4 6 7 8
1 2 3 4 5 6 7 8
A 2 5 3 0 2 3 0 3
j
1 2 3 4 5 6 7 8 0 1 2 3 4 5
B 0 3 3 C 1 2 4 5 7 8
Counting Sort
for j=A. length down to 1
B[C[ A[j] ]] = A[j];
1 2 3 4 5 6 7 8
C[ A[j] ] = C[ A[j] ] - 1;
A 2 5 3 0 2 3 0 3
j
1 2 3 4 5 6 7 8 0 1 2 3 4 5
B 0 2 3 3 C 1 2 4 5 7 8
Counting Sort
for j=A. length down to 1
B[C[ A[j] ]] = A[j];
1 2 3 4 5 6 7 8
C[ A[j] ] = C[ A[j] ] - 1;
A 2 5 3 0 2 3 0 3
j
1 2 3 4 5 6 7 8 0 1 2 3 4 5
B 0 2 3 3 C 1 2 4 5 7 8
1 2 3 4 5 6 7 8
A 2 5 3 0 2 3 0 3
j
1 2 3 4 5 6 7 8 0 1 2 3 4 5
B 0 2 3 3 C 1 2 3 5 7 8
Counting Sort
for j=A. length down to 1
B[C[ A[j] ]] = A[j];
C[ A[j] ] = C[ A[j] ] - 1; 1 2 3 4 5 6 7 8
A 2 5 3 0 2 3 0 3
j
1 2 3 4 5 6 7 8 0 1 2 3 4 5
B 0 0 2 3 3 C 1 2 3 5 7 8
Counting Sort
for j=A. length down to 1
B[C[ A[j] ]] = A[j];
C[ A[j] ] = C[ A[j] ] - 1; 1 2 3 4 5 6 7 8
A 2 5 3 0 2 3 0 3
j
1 2 3 4 5 6 7 8 0 1 2 3 4 5
B 0 0 2 3 3 C 1 2 3 5 7 8
1 2 3 4 5 6 7 8
A 2 5 3 0 2 3 0 3
j
1 2 3 4 5 6 7 8 0 1 2 3 4 5
B 0 0 2 3 3 C 0 2 3 5 7 8
Counting Sort
for j=A. length down to 1
B[C[ A[j] ]] = A[j];
C[ A[j] ] = C[ A[j] ] - 1; 1 2 3 4 5 6 7 8
A 2 5 3 0 2 3 0 3
j
1 2 3 4 5 6 7 8 0 1 2 3 4 5
B 0 0 2 3 3 3 C 0 2 3 5 7 8
Counting Sort
for j=A. length down to 1
B[C[ A[j] ]] = A[j];
C[ A[j] ] = C[ A[j] ] - 1; 1 2 3 4 5 6 7 8
A 2 5 3 0 2 3 0 3
j
1 2 3 4 5 6 7 8 0 1 2 3 4 5
B 0 0 2 3 3 3 C 0 2 3 5 7 8
1 2 3 4 5 6 7 8
A 2 5 3 0 2 3 0 3
j
1 2 3 4 5 6 7 8 0 1 2 3 4 5
B 0 0 2 3 3 3 C 0 2 3 4 7 8
Counting Sort
for j=A. length down to 1
B[C[ A[j] ]] = A[j];
C[ A[j] ] = C[ A[j] ] - 1; 1 2 3 4 5 6 7 8
A 2 5 3 0 2 3 0 3
j
1 2 3 4 5 6 7 8 0 1 2 3 4 5
B 0 0 2 3 3 3 5 C 0 2 3 4 7 8
Counting Sort
for j=A. length down to 1
B[C[ A[j] ]] = A[j];
C[ A[j] ] = C[ A[j] ] - 1; 1 2 3 4 5 6 7 8
A 2 5 3 0 2 3 0 3
j
1 2 3 4 5 6 7 8 0 1 2 3 4 5
B 0 0 2 3 3 3 5 C 0 2 3 4 7 8
1 2 3 4 5 6 7 8
A 2 5 3 0 2 3 0 3
j
1 2 3 4 5 6 7 8 0 1 2 3 4 5
B 0 0 2 3 3 3 5 C 0 2 3 4 7 7
Counting Sort
for j=A. length down to 1
B[C[ A[j] ]] = A[j];
1 2 3 4 5 6 7 8
C[ A[j] ] = C[ A[j] ] - 1;
A 2 5 3 0 2 3 0 3
j
1 2 3 4 5 6 7 8 0 1 2 3 4 5
B 0 0 2 2 3 3 3 5 C 0 2 3 4 7 7
Counting Sort
for j=A. length down to 1
B[C[ A[j] ]] = A[j];
1 2 3 4 5 6 7 8
C[ A[j] ] = C[ A[j] ] - 1;
A 2 5 3 0 2 3 0 3
j
1 2 3 4 5 6 7 8 0 1 2 3 4 5
B 0 0 2 2 3 3 3 5 C 0 2 3 4 7 7
1 2 3 4 5 6 7 8
A 2 5 3 0 2 3 0 3
j
1 2 3 4 5 6 7 8 0 1 2 3 4 5
B 0 0 2 2 3 3 3 5 C 0 2 2 4 7 7
Counting Sort
Counting-Sort(A, B, k)
1. Let C[0…..k] be a new array
2. for i=0 to k
3. C[i]= 0;
4. for j=1 to A. length
5. C[ A[j] ] = C[ A[j] ] + 1;
6. for i=1 to k
7. C[i] = C[i] + C[i-1];
8. for j=A. length down to 1
9. B[C[ A[j] ]] = A[j];
10. C[ A[j] ] = C[ A[j] ] - 1;
Counting Sort
Counting-Sort(A, B, k)
1. Let C[0…..k] be a new array
2. for i=0 to k [Loop 1]
3. C[i]= 0;
4. for j=1 to A. length [Loop 2]
5. C[ A[j] ] = C[ A[j] ] + 1;
6. for i=1 to k [Loop 3]
7. C[i] = C[i] + C[i-1];
8. for j=A. length down to 1 [Loop 4]
9. B[C[ A[j] ]] = A[j];
10. C[ A[j] ] = C[ A[j] ] - 1;
Complexity Analysis
Counting-Sort(A, B, k)
1. Let C[0…..k] be a new array
2. for i=0 to k [Loop 1]
3. C[i]= 0;
4. for j=1 to A. length [Loop 2]
5. C[ A[j] ] = C[ A[j] ] + 1;
6. for i=1 to k [Loop 3]
7. C[i] = C[i] + C[i-1];
8. for j=A. length down to 1 [Loop 2]
9. B[C[ A[j] ]] = A[j];
10. C[ A[j] ] = C[ A[j] ] - 1;
Complexity Analysis
• So the counting sort takes a total time of: O(n + k)
• Counting sort is called stable sort.
( A sorting algorithm is stable when numbers with the
• Pro’s
• Asymptotically fast Fast - O(n + k)
• Simple to code
• Con’s
• Doesn’t sort in place.
• Requires O(n + k) extra storage space.
Linear Time Sorting
(Radix Sort)
Overview
• Running time of counting sort is
• Required extra space for sorting.
• Is a stable sorting.
Radix Sort
• Radix sort is non comparative
sorting method
• Two classifications of radix sorts are
least significant digit (LSD) radix
sorts and most significant digit
(MSD) radix sorts.
• LSD radix sorts process the integer
representations starting from the
least digit and move towards the
most significant digit. MSD radix
sorts work the other way around.
Radix Sort (Algorithm)
Radix_Sort(A,d)
Radix Sort
•In input array A, each element is a number of d
digit.
329
457
657
839
436
720
355
Radix Sort
•In input array A, each element is a number of d
digit.
329 720
457 355
657 436
839 457
436 657
720 329
355 839
Radix Sort
•In input array A, each element is a number of d
digit.
do MAX-HEAPIFY
(A,i,n)
6 5
i
0 8 2 1
1 2 3 4 5 6 7 8
9 6 5 0 8 2 1 3
3
Tighter analysis Proof
• For easy understanding, Let us take a complete
binary Tree,
…
Number 281942902 Number 233667136 Number 580625685
Number 701466868 Number 506643548 Number 155778322
3 6 7 11 32 33 53
Binary
Search
Example: sorted array of integer keys. Target=7.
3 6 7 11 32 33 53
3 6 7 11 32 33 53
3 6 7 11 32 33 53
3 6 7 11 32 33 53
3 6 7 11 32 33 53
3 6 7 11 32 33 53
3 6 7 11 32 33 53
3 6 7 11 32 33 53
3 6 7 11 32 33 53
3 6 7 11 32 33 53
11
6 33
3 7 32 53
Search for target = 7
Find midpoint:
3 6 7 11 32 33 53
Start at root:
11
6 33
3 7 32 53
Search for target = 7
Search left subarray:
3 6 7 11 32 33 53
3 7 32 53
Search for target = 7
Find approximate midpoint of
subarray:
3 6 7 11 32 33 53
3 7 32 53
Search for target = 7
Search right subarray:
3 6 7 11 32 33 53
3 7 32 53
Binary Search: Analysis
• Worst case complexity?
• What is the maximum depth of recursive calls in binary
search as function of n?
• Each level in the recursion, we split the array in half
(divide by two).
• Therefore maximum recursion depth is floor(log2n) and
worst case = O(log2n).
• Average case is also = O(log2n).
Hash Tables
• Constant time accesses! hash table
• A hash table is an array of some
fixed size, usually a prime number.0
• General idea:
hash function:
h(K)
…
•
TableSize = 10
h(K) = K mod 10
1
• Insert: 7, 18, 41, 94 2
3
4
5
6
7
8
9
313
Another Example
• key space = integers
• TableSize = 6
• h(K) = K mod 6 0
• Insert: 7, 18, 41, 34
1
2
3
4
5
314
Hash Functions
1. simple/fast to compute,
2. Avoid collisions
3. have keys distributed evenly among
cells.
315
Sample Hash Functions:
• key space = strings
• s = s0 s1 s2 … s k-1
317
Separate Chaining
Insert:
0 10
1 22
107
2 12
3 42
4 • Separate
5 chaining: All keys
6 that map to the
same hash value
7 are kept in a list
8 (or “bucket”).
9
318
Analysis of find
• Defn: The load factor, , of a hash table
is the ratio:N no. of elements
M table size
For separate chaining, = average # of
elements in a bucket
• Unsuccessful find:
• Successful find:
319
How big should the hash table be?
• For Separate Chaining:
320
tableSize: Why
Prime?
• Suppose
• data stored in hash table: 7160, 493, 60, 55,
321, 900, 810
321
Open Addressing
Insert:
38
0 19
1 8
2 109
10
3
4 • Linear Probing:
5 after checking
6 spot h(k), try spot
h(k)+1, if that is
7
full, try h(k)+2,
8 then h(k)+3, etc.
9
322
Terminology Alert!
323
Linear Probing
f(i) = i
• Probe sequence:
0th probe = h(k) mod TableSize
1th probe = (h(k) + 1) mod TableSize
2th probe = (h(k) + 2) mod TableSize
...
ith probe = (h(k) + i) mod TableSize
324
g–
Cluster
ing
no collision
collision in small cluster
no collision
[R. Sedgewick]
325
Load Factor in Linear
Probing
• For any < 1, linear probing will find an empty slot
• Expected # of probes (for large table sizes)
• successful search:
1 1
1
• unsuccessful search: 2 1
1 1
1
• Linear probing suffers from 1 clustering
2 primary 2
326
Quadratic Probing Less likely to
encounter
Primary
f(i) = i2
Clustering
• Probe sequence:
0th probe = h(k) mod TableSize
1th probe = (h(k) + 1) mod TableSize
2th probe = (h(k) + 4) mod TableSize
3th probe = (h(k) + 9) mod TableSize
...
ith probe = (h(k) + i2) mod TableSize
327
Quadratic Probing
0 Insert:
1 89
18
2
49
3 58
4 79
5
6
7
8
9
328
Quadratic
insert(76)
Probing
insert(40) insert(48)
Example
insert(5) insert(55)
76%7 = 6 40%7 = 5 48%7 = 6 5%7 = 5 55%7 = 6
0
But… insert(47)
1
47%7 = 5
2
6
76
329
Success guarantee
for < ½
• If size is prime and < ½, then quadratic
probing will find an empty slot in size/2
probes or fewer.
• show for all 0 i,j size/2 and i j
(h(x) + i2) mod size (h(x) + j2) mod size
• by contradiction: suppose that for some i j:
(h(x) + i2) mod size = (h(x) + j2) mod size
i2 mod size = j2 mod size
(i2 - j2) mod size = 0
[(i + j)(i - j)] mod size = 0
331
Double Hashing
f(i) = i * g(k)
where g is a second hash function
• Probe sequence:
0th probe = h(k) mod TableSize
1th probe = (h(k) + g(k)) mod TableSize
2th probe = (h(k) + 2*g(k)) mod TableSize
3th probe = (h(k) + 3*g(k)) mod TableSize
...
ith probe = (h(k) + i*g(k)) mod TableSize
332
g
Exampl
e h(k) = k mod 7 and g(k) = 5 – (k mod 5)
76 93 40 47 10 55
0 0 0 0 0 0
1 1 1 1 47 1 47 1 47
2 2 93 2 93 2 93 2 93 2 93
3 3 3 3 3 10 3 10
4 4 4 4 4 4 55
5 5 5 40 5 40 5 40 5 40
6 76 6 76 6 76 6 76 6 76 6 76
Probes 1 1 1 2 1 2
333
Resolving Collisions with Double
Hashing
0 Hash Functions:
1 H(K) = K mod M
2 H2(K) = 1 + ((K/M) mod
(M-1))
3
M=
4 Insert these values into the hash table
5 in this order. Resolve any collisions
with double hashing:
6 13
7 28
8 33
9 147
43
334
Rehashing
Idea: When the table gets too full, create
a bigger table (usually 2x as large) and
hash all the items from the original table
into the new table.
• When to rehash?
• half full ( = 0.5)
• when an insertion fails
• some other threshold
• Cost of rehashing?
335
Java hashCode()
Method
• Class Object defines a hashCode method
• Intent: returns a suitable hashcode for the
object
• Result is arbitrary int; must scale to fit a
hash table (e.g. [Link]() % nBuckets)
• Used by collection classes like HashMap
• Classes should override with calculation
appropriate for instances of the class
• Calculation should involve semantically
“significant” fields of objects
336
hashCode() and equals()
• To work right, particularly with collection classes like
HashMap, hashCode() and equals() must obey this rule:
if [Link](b) then it must be true that
[Link]() == [Link]()
• Why?
• Reverse is not required
337