Lecture11 Updated
Lecture11 Updated
Lecture #11
• Sorting Algorithms, part II:
– Quicksort
– Mergesort
• Trees
– Introduction
– Implementation & Basic Properties
– Traversals: The Pre-order Traversal
• On-your-own Study
– Full binary trees
2
So pay attention!
6
Any time you see “divide and conquer,” you should think
recursion... EEK!
7
30
13 1 77 13 69 40 77
21 30 21
QuickSort
8
Select an arbitrary item P from the array.
Move items smaller than or equal to P to the left and
larger items to the right; P goes in-between.
Recursively repeat this process on the left items
Recursively repeat this process on the right items
P
Everything on this side
is smaller than item P!
Everything on this side
is larger than item P!
EE MBA History Bio Drop-out CS USC
Major Major Major Major Grad
QuickSort
9
Select an arbitrary item P from the array.
Select an arbitrary item P from the array.
Move items smaller than or equal to P to the left and
Move items
larger smaller
items to thethan or Pequal
right; goesto P to the left and
in-between.
larger items to the right; P goes in-between.
Recursively repeat this process on the left items
Recursively repeat this process on the left items
Recursively repeat this process on the right items
Recursively repeat this process on the right items
P2 P
History Bio USC EE MBA Drop-out CS
Major Major Grad Major Major
P3
P2 P
USC History Bio EE MBA Drop-out CS
Grad Major Major Major Major
Everything right
Finally, all items are sorted!
of EE Major
(our first P) is now
sorted!
D&C Sorts:LastQuicksort
11
First
specifies thespecifies the
Only bother sorting
And here’s arrays
an actual Quicksort
oflast element
at least twostarting
C++ element of
function:
of the
the array to sort.
array to sort.
elements! 0
DIVIDE 7
CONQUER Pick an element.
void QuickSort(int Array[],int First,int Last)
{
CONQUER
Apply Move <= items left
our
if (Last – First >= 1 QS
)
{ algorithm toMove
Apply our > items right
QS left
the
algorithm to the
half of the
int PivotIndex; right
array.
3 half of the array.
PivotIndex = Partition(Array,First,Last);
QuickSort(Array,First,PivotIndex-1); // left
QuickSort(Array,PivotIndex+1,Last); // right
}
}
30
13 1 77
21 30
13 69 40 77
21 46
0 1 2 3 4 5 6 7
12
Big-oh of Quicksort
n steps
We first partition the
array, at a cost of n steps.
30
13 1 77 13 69 40 77
21 30 21 46
Then we repeat the log2(n)
process for each half… levels n steps
We partition each of the 2
13
1 131 21
This is our pivot value.
halves, each taking n/2 steps, 69
40 40 69 46
46 77 77
Our partition function
at a total cost of n steps.
moves smaller values to the n steps
left of the array, larger
Then we repeat thevalues to the right.
process for each half… 1 21 40 46 77
1 10 20 30 40 50 60 70
It’s a DOG!
19
QuickSort Questions
Can QuickSort be applied easily to
sort items within a linked list?
i1
Ok, let’s look at the
C++ code for the A2
1. Initialize counter variables i1, i2 to zero
Merge Algorithm
2. While there are more items to copy…
merge algorithm! Consider
If A1[i1]the left-most
is less book in both shelves
than A2[i2]
Take theCopysmallest
A1[i1] to of the array
output two books
B and i1++
Add Else
it to the new shelf
Repeat Copy
theA2[i2]
wholetoprocess
output array B and
until all i2++
books
3.are
If either
movedarray runs out, copy the entire
contents of the other array over
i2
22
Mergesort
OK – so what’s the full mergesort alogrithm:
Mergesort function :
1. If array has one element, then return (it’s sorted).
2. Split up the array into two equal sections
3. Recursively call Mergesort function on the left half
4. Recursively call Mergesort function on the right half
5. Merge the two halves using our merge function
1. If array
1. Ifhas 1 item,
array has 1then
item,return
then return
2. Split2.array
Splitinarray
two equal
in twosections
equal sections
3. Call 3.
Mergesort on the on
Call Mergesort left half
the left half
4. Call 4.
Mergesort on the on
Call Mergesort right
thehalf
right half
5. Merge the halves
5. Merge back together
the halves back together
i1 i2
25
To save time, we’ll skip
1. If array
thehas 1 item,
tracing on then return
this side…
2. Split array in two equal sections
3. Call Mergesort on the left half
4. Call Mergesort on the right half
5. Merge the halves back together
i1 i2
26
i1 i2
The real algorithm sorts the data in-place
in the array…
Mergesort
n items merged
Big-oh of
29
Mergesort
n items merged
Big-oh of
30
Mergesort
n items merged
log2n levels deep
Why? Because we
n items merged keep dividing our
piles in half…
until our piles are
just 1 book!
n items merged
MergeSort Questions
Insertion Stable O(n) for already or nearly-ordered arrays. O(n 2) otherwise. Can be
Sort used with linked lists. Easy to implement.
Quick Unstable O(n log2n) average, O(n2) for already/mostly/reverse ordered arrays or
Sort arrays with the same value repeated many times. Can be used with
linked lists. Can be parallelized across multiple cores. Can require up
O(n) slots of extra RAM (for recursion) in the worst case, O(log 2n) avg.
Merge Stable O(n log2n) always. Used for sorting large amounts of data on disk
Sort (aka “external sorting”). Can be used to sort linked lists. Can be
parallelized across multiple cores. Downside: Requires n slots of
34
Challenge Problems
Databases (B-TREES)
Data Compression (Huffman Trees)
Bitcoin (Merkle Trees)
Medical Diagnosis (Decision Trees)
So pay attention!
Trees
38
A Decision Tree
AAn A Does
Binary
Family theTree
Search Tree
• To organize hierarchical data Expression
patient
Tree
• To make information easily have a+fever?
“marty”
“carey”
searchable yes no
• To simplify the evaluation of Does he/she Does he/she
mathematical expressions have “leon”
spots
* on
“harry”
his/her face?
have +
a sore
“andrea”
“rich”
throat?
• To make decisions
yes
“sheila”
“alan”
He has 32 …
“simon”
“jacob” -42 “milton”
17 “nancy”
“martha” “zai”
4
COOTIES!
NULL NULL
NULL NULL NULL
NULL NULL NULL NULL
NULL NULL
NULL NULL NULL NULL
NULL
Basic Tree Facts
39
node *rootPtr;
40
struct node
{
int value; // node data 3
node *pChildren[26]; NULL
};
7 4 15
NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL
Binary Trees
41
“ziggy”
NULL NULL
Binary Tree Subtrees
43
“carey” Carey’s
like this node…
right
Carey’s subtree
left “leon” “andrea”
subtree
“ziggy”
NULL NULL
44
reserve 12 bytes
temp->left = NULL;
temp->right = NULL;
of memory
temp-> for
value 7
1200
pRoot->left = temp;
me? left right temp-> value -3 1100
temp = new BTNODE;
temp->value = -3; NULL NULL Sure, here’s a
left right
temp->left = NULL; chunk ofNULL memory
NULL
temp->right = NULL; at address 1200.
1100.
1000.
pRoot->right = temp; And of course, later we’d have
// etc…
to delete our tree’s nodes.
We’ve created a binary tree…
46
now what?
Now that we’ve created a
binary tree, what can we
do with it?
Well, next class we’ll learn
how to use the binary tree to
speed up searching for data.
But for now, let’s learn how
to iterate through each item
in a tree, one at a time.
This is called “traversing” the
tree, and there are several
ways to do it.
47
sub-tree.
3. Process the nodes in the right “d” “e”
sub-tree. NULL NULL NULL NULL
“d” e
“e”
cur
NULL NULL NULL NULL
“d” “e”
NULL NULL NULL NULL
“d” “e”
NULL NULL NULL NULL
All of the 2
Depth: “sheila” “simon” “martha” “milton” Are at the
leaf nodes… NULL NULL NULL NULL NULL NULL NULL NULL
same depth!
Full Binary Trees
57
“carey”
“carey”
“leon” “andrea”
“leon” “andrea” NULL NULL NULL
NULL NULL NULL NULL
Is it full?
“carey”
NULL NULL