Professional Documents
Culture Documents
Diving Into Algorithms: Presentation by
Diving Into Algorithms: Presentation by
Presentation by
Rajmohan De Sarkar
Apr 18, 2012
Quicksort
To sort a[left...right]:
1. if left < right:
Partitioning
A key step in the Quicksort algorithm is
partitioning the array
We choose some (any) number p in the
array to use as a pivot
We partition the array into three parts:
p
numbers less
than p
Partitioning
Choose an array value (say, the first) to use as
the pivot
Starting from the left end, find the first
element that is greater than or equal to the
pivot
Searching backward from the right end, find
the first element that is less than the pivot
Interchange (swap) these two elements
Repeat, searching from where we left off, until
done
Partitioning
To partition a[left...right]:
1. Set pivot = a[left], l = left + 1, r = right;
2. while l < r, do
Example of partitioning
choose pivot:
search:
swap:
search:
swap:
search:
swap:
search:
4
4
4
4
4
4
4
4
3
3
3
3
3
3
3
3
6
6
3
3
3
3
3
3
9
9
9
9
1
1
1
1
2
2
2
2
2
2
2
2
4
4
4
4
4
4
2
2
3
3
3
3
3
3
3
3
1
1
1
1
1
1
1
1
2
2
2
2
2
2
4
4
1
1
1
1
9
9
9
9
8
8
8
8
8
8
8
8
9
9
9
9
9
9
9
9
3
3
6
6
6
6
6
6
5
5
5
5
5
5
5
5
6
6
6
6
6
6
6
6
(left
> right)
133122344989656
Apr 18, 2012
= array[left];
array[left] = array[right];
array[right] = temp;
}
}
else {
int p = partition(array, left, right);
quicksort(array, left, p - 1);
quicksort(array, p + 1, right);
}
}
}
10
11
Best case II
We cut the array size in half each time
So the depth of the recursion in log2n
At each level of the recursion, all the partitions at
that level do work that is linear in n
O(log2n) * O(n) = O(n log2n)
Hence in the average case, quicksort has time
complexity O(n log2n)
What about the worst case?
12
Worst case
In the worst case, partitioning always divides the
size n array into these three parts:
A length one part, containing the pivot itself
A length zero part, and
A length n-1 part, containing everything else
13
14
15
16
Final comments
Quicksort is the fastest known sorting algorithm
For optimum efficiency, the pivot must be chosen
carefully
Median of three is a good technique for choosing
the pivot
However, no matter what you do, there will be
some cases where Quicksort runs in O(n2) time
17
19
6
5
4
3
2
1
Compare
We start by comparing the first
two elements in the List.
This list is an example of a worst
case scenario for the Bubble
Sort, because the List is in the
exact opposite order from the way
we want it sorted (the computer
program does not know that it is
sorted at all).
Apr 18, 2012
20
Swap
21
Compare
22
Swap
23
Compare
24
Swap
25
Compare
26
Swap
27
Compare
28
Swap
29
Compare
For our second pass through
the List, we start by
comparing these first two
elements in the List.
30
Swap
31
Compare
32
Swap
33
Compare
34
Swap
35
Compare
36
37
Compare
We start with the first two
elements again at the beginning of
the third pass.
38
Swap
39
Compare
40
Swap
41
Compare
42
43
Compare
The beginning of the fourth pass...
44
Swap
45
Compare
46
Swap
The end of the fourth pass
stops at element number n - 3.
47
Compare
48
Swap
The last pass compares only
the first two elements of the
List. After this comparison
and possible swap, the
smallest element has
bubbled to the top.
49
TEMP
6
Place the first element into the
Temporary Variable.
50
TEMP
6
Replace the first element with
the second element.
51
TEMP
6
Replace the second element with
the Temporary Variable.
52
53
54
Big - O Notation
Big - O notation is used to describe the efficiency
of a search or sort. The actual time necessary to
complete the sort varies according to the speed of
your system. Big - O notation is an approximate
mathematical formula to determine how many
operations are necessary to perform the search or
sort. The Big - O notation for the Bubble Sort is
O(n2), because it takes approximately n2 passes to
sort the elements.
55
Insertion Sort
Insertion Sort
Consists of N-1 passes
For pass P = 1 through N-1, insertion sort ensures
that the elements in positions 0 through P are in
the sorted order
In pass P, the element in the position P is moved
left until its correct place is found among the first
P + 1 elements.
Complexity - O(n2)
57
34
64 51 32 21
58
34
64 51 32 21
59
34
64 51 32 21
60
34
64 51 32 21
61
34
64 51 32 21
62
34
64 51 32 21
63
34
64 51 32 21
64
34
64 51 32 21
8
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012
65
34
64 51 32 21
8
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012
66
34
64 51 32 21
8
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012
67
34 64 51 32 21
8
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012
68
34 64 51 32 21
8
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012
69
34 64 51 32 21
8
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012
70
34 64 51 32 21
8
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012
71
34 64 51 32 21
72
34 64 51 32 21
73
34 64 51 32 21
74
34 64 51 32 21
75
34 64 51 32 21
76
34
51 32 21
64
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012
77
34
51 32 21
64
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012
78
34
51 32 21
64
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012
79
34 64 51 32 21
80
34 64 51 32 21
81
34 64 51 32 21
82
34 64 51 32 21
83
34 64 51 32 21
84
34 64
32 21
51
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012
85
34 64
32 21
51
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012
86
34 64
32 21
51
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012
87
34
64 32 21
51
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012
88
34
64 32 21
51
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012
89
34
64 32 21
51
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012
90
34
64 32 21
51
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012
91
34 51 64 32 21
92
34 51 64 32 21
93
34 51 64 32 21
94
34 51 64 32 21
95
34 51 64 32 21
96
34 51 64 32 21
97
34 51 64
21
32
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012
98
34 51 64
21
32
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012
99
34 51 64
21
32
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012
100
34 51
64 21
32
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012
101
34 51
64 21
32
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012
102
34 51
64 21
32
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012
103
34 51
64 21
32
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012
104
34
51 64 21
32
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012
105
34
51 64 21
32
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012
106
34
51 64 21
32
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012
107
34
51 64 21
32
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012
108
34 51 64 21
32
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012
109
34 51 64 21
32
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012
110
34 51 64 21
32
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012
111
34 51 64 21
32
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012
112
32 34 51 64 21
113
32 34 51 64 21
114
32 34 51 64 21
115
32 34 51 64 21
116
32 34 51 64 21
117
32 34 51 64 21
118
32 34 51 64
21
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012
119
32 34 51 64
21
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012
120
32 34 51 64
21
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012
121
32 34 51
64
21
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012
122
32 34 51
64
21
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012
123
32 34 51
64
21
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012
124
32 34 51
64
21
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012
125
32 34
51 64
21
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012
126
32 34
51 64
21
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012
127
32 34
51 64
21
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012
128
32 34
51 64
21
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012
129
32
34 51 64
21
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012
130
32
34 51 64
21
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012
131
32
34 51 64
21
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012
132
32
34 51 64
21
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012
133
32 34 51 64
21
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012
134
32 34 51 64
21
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012
135
32 34 51 64
21
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012
136
32 34 51 64
21
void InsertionSort(ElementType A[ ], int N)
{
int j, P;
ElementType Tmp;
for(P = 1;P<N; P++)
{
Tmp = A[P];
for (j = P;j>0 && A[j-1] > Tmp; j--)
A[ j] = A[j-1];
A[j] = Tmp;
}
}
Apr 18, 2012
137
21 32 34 51 64
138
21 32 34 51 64
139
21 32 34 51 64
140
21 32 34 51 64
141
21 32 34 51 64
142
144
145
6
2
1
3
5
4
Smallest ?
We start by searching for the
smallest element in the List.
146
6
2
1
3
5
4
147
Smallest !
148
Swap
6
2
1
3
5
4
149
Swapped
150
1
2
6
3
5
4
151
Smallest !
In this special case, the
next smallest element is in
the second position
already. Swapping keeps it
in this position.
Apr 18, 2012
152
Swapped
Swapping keeps it in this
position.
153
Smallest ?
After the next smallest element is in
the second position, we continue
searching with the third element and
look for the next smallest element.
1
2
6
3
5
4
154
Smallest !
155
Swap
1
2
6
3
5
4
156
Swapped
1
2
3
6
5
4
157
Smallest ?
1
2
3
6
5
4
158
Smallest ?
1
2
3
6
5
4
159
Smallest !
Apr 18, 2012
160
Swap
Apr 18, 2012
1
2
3
6
5
4
161
Swapped
Apr 18, 2012
162
163
TEMP
6
Place the first element into
the Temporary Variable.
164
TEMP
6
Replace the first element
with the value of the
smallest element.
165
TEMP
6
Replace the third element
(in this example) with the
Temporary Variable.
166
167
168
169
Big - O Notation
Big - O notation is used to describe the
efficiency of a search or sort. The actual
time necessary to complete the sort varies
according to the speed of your system. Big
- O notation is an approximate mathematical
formula to determine how many operations
are necessary to perform the search or sort.
The Big - O notation for the Selection Sort is
O(n2), because it takes approximately n2
passes to sort the elements.
170
Heapsort
The Heap
A binary heap is a nearly complete binary tree
Implemented as an array A
Two similar attributes:
length[A] is the size (number of slots) in A
heap-size[A] is the number of elements in A
Thus, heap-size[A] length[A]
Also, no element past A[heap-size[A]] is an
element
172
The Heap
25
67
41
55
43
87
21
87
17
33
35
51 67 25 41
55
43
21 17 33 35
Apr 18, 2012
173
Simple Functions
PARENT(i)
return (i/2)
LEFT(i)
return (2i)
RIGHT(i)
return (2i + 1)
174
Properties
Max-heap property:
A[PARENT(i)] A[i]
Min-heap property:
A[PARENT(i)] A[i]
Max-heaps are used for sorting
Min-heaps are used for priority queues (later)
We define the height of a node to be the longest path from
the node to a leaf.
The height of the tree is (lg n)
175
MAX-HEAPIFY
This is the heart of the algorithm
Determines if an individual node is smaller than its
children
Parent swaps with largest child if that child is
larger
Calls itself recursively
Runs in O(lg n) or O(h)
176
HEAPIFY
MAX-HEAPIFY (A, i)
l LEFT (i)
r RIGHT(i)
if l heap-size[A] and A[l] > A[i]
then largest l
else largest i
if r heap-size[A] and A[r]>A[largest]
then largest r
if largest i
then exchange A[i] with A[largest]
MAX-HEAPIFY (A, largest)
Apr 18, 2012
177
16
10
14
178
16
14
10
179
16
14
10
180
BUILD-HEAP
Use MAX-HEAPIFY in bottom up manner
Why does the loop start at length[A]/2?
At the start of each loop, each node i is the
root of a max-heap!
BUILD-HEAP (A)
heap-size[A] length[A]
for i length[A]/2 downto 1
do MAX-HEAPIFY(A, i)
Apr 18, 2012
181
HEAPSORT
HEAPSORT (A)
BUILD-HEAP(A)
for i length[A] downto 2
do exchange A[1] with A[i]
heap-size[A] heap-size[A] - 1
MAX-HEAPIFY(A, 1)
182
16
14
10
183
14
10
16
184
14
10
16
heap-size heap-size 1
MAX-HEAPIFY(A, 1)
185
10
14
16
186
10
14
16
heap-size heap-size 1
MAX-HEAPIFY(A, 1)
187
10
14
16
188
10
14
16
heap-size heap-size 1
MAX-HEAPIFY(A, 1)
189
10
14
16
190
10
14
16
heap-size heap-size 1
MAX-HEAPIFY(A, 1)
191
10
14
16
192
10
14
16
heap-size heap-size 1
MAX-HEAPIFY(A, 1)
193
10
14
16
194
10
14
16
heap-size heap-size 1
MAX-HEAPIFY(A, 1)
195
10
14
16
196
10
14
16
heap-size heap-size 1
MAX-HEAPIFY(A, 1)
197
10
14
16
198
10
14
16
heap-size heap-size 1
MAX-HEAPIFY(A, 1)
199
10
14
16
200
10
14
16
heap-size heap-size 1
MAX-HEAPIFY(A, 1)
201
C/C++ Code
void MAX_HEAPIFY (int A[], int i) {
int l = LEFT(i);
int r = RIGHT(i);
int largest;
if (l <= heapsize && A[l] > A[i])
largest = l;
else
largest = i;
if (r <= heapsize && A[r]>A[largest])
largest = r;
if (largest != i) {
exchange (&A[i], &A[largest]);
MAX_HEAPIFY (A, largest);
}
}
202
C/C++ Code
void BUILD_HEAP (int A []) {
heapsize = length;
for (int i = length/2;i >= 1; --i)
MAX_HEAPIFY(A, i);
}
void HEAPSORT (int A[]) {
BUILD_HEAP(A);
for (int i = length;i >= 2; --i) {
exchange (&A[1], &A[i]);
heapsize = heapsize - 1;
MAX_HEAPIFY(A, 1);
}
}
Apr 18, 2012
203
C/C++Code
int length = 5;
int heapsize = 0;
#define LEFT(i) 2*i
#define RIGHT(i) (2*i + 1)
void exchange (int *number1, int *number2) {
int temp;
temp = *number1;
*number1 = *number2;
*number2 = temp;
}
void main () {
int A[] = {0, 9999, 8888, 7777, 6666, 5555};
HEAPSORT (A);
for (int i = 1; i <= 5; ++i)
printf ("%d\n", A [i]);
}
204
205
Mergesort
Merge Sort
Apply divide-and-conquer to sorting problem
Problem: Given n elements, sort elements into nondecreasing order
Divide-and-Conquer:
If n=1 terminate (every one-element list is already
sorted)
If n>1, partition elements into two sub-collections; sort
each; combine into a single sorted list
How do we partition?
207
The Process
Divide the unsorted array into 2 halves until
the sub-arrays only contain one element
Merge the sub-problem solutions together:
Compare the sub-arrays first elements
Remove the smallest element and put it into
the result array
Continue the process until all elements have
been put into the result array
208
Mergesort: Illustration
85
24
63
45
17
31
96
50
209
Mergesort: Illustration
85
24
63
45
17
31
96
50
210
Mergesort: Illustration
17
85
24
63
31
96
50
45
211
Mergesort: Illustration
17
63
85
31
96
50
45
24
212
Mergesort: Illustration
17
63
85
31
96
50
45
24
213
Mergesort: Illustration
17
24
63
31
96
50
45
85
214
Mergesort: Illustration
17
24
63
31
96
50
45
85
215
Mergesort: Illustration
17
24
85
63
31
96
50
45
216
Mergesort: Illustration
17
24
85
63
31
96
50
45
217
Mergesort: Illustration
17
24
85
63
31
96
50
45
218
Mergesort: Illustration
17
24
31
96
50
85
63
45
219
Mergesort: Illustration
17
24
31
96
50
85
63
45
220
Mergesort: Illustration
17
24
85
31
96
50
45
63
221
Mergesort: Illustration
17
24
85
31
96
50
45
63
222
Mergesort: Illustration
17
24
85
45
31
96
50
63
223
Mergesort: Illustration
17
24
85
45
31
96
50
63
224
Mergesort: Illustration
17
24
85
45
31
96
50
63
225
Mergesort: Illustration
17
24
85
45
31
96
50
63
226
Mergesort: Illustration
24
17
85
45
31
96
50
63
227
Mergesort: Illustration
24
17
85
45
31
96
50
63
228
Mergesort: Illustration
24
45
85
17
31
96
50
63
229
Mergesort: Illustration
24
45
85
17
31
96
50
63
230
Mergesort: Illustration
24
45
63
17
31
96
50
85
231
Mergesort: Illustration
24
45
63
17
31
96
50
85
232
Mergesort: Illustration
24
45
63
85
17
31
96
50
233
Mergesort: Illustration
24
45
63
85
17
31
96
50
234
Mergesort: Illustration
24
45
63
85
17
31
96
50
235
Mergesort: Illustration
24
45
63
85
17
31
96
50
236
Mergesort: Illustration
24
45
63
85
96
17
50
31
237
Mergesort: Illustration
24
45
63
85
96
17
50
31
238
Mergesort: Illustration
24
45
63
85
17
96
50
31
239
Mergesort: Illustration
24
45
63
85
17
96
50
31
240
Mergesort: Illustration
24
45
63
85
17
31
96
50
241
Mergesort: Illustration
24
45
63
85
17
31
96
50
242
Mergesort: Illustration
24
45
63
85
17
31
96
50
243
Mergesort: Illustration
24
45
63
85
17
31
96
50
244
Mergesort: Illustration
24
45
63
85
17
31
96
50
245
Mergesort: Illustration
24
45
63
85
17
31
50
96
246
Mergesort: Illustration
24
45
63
85
17
31
50
96
247
Mergesort: Illustration
24
45
63
85
17
31
50
96
248
Mergesort: Illustration
24
45
63
85
17
31
50
96
249
Mergesort: Illustration
24
45
63
85
17
31
50
96
250
Mergesort: Illustration
24
45
63
85
17
31
50
96
251
Mergesort: Illustration
24
45
63
85
17
31
50
96
252
Mergesort: Illustration
24
45
63
85
17
31
50
96
253
Mergesort: Illustration
24
45
63
85
17
31
50
96
254
Mergesort: Illustration
24
45
63
85
17
31
50
96
255
Mergesort: Illustration
24
45
63
85
17
31
50
96
256
Mergesort: Illustration
24
45
63
85
17
31
50
96
257
Mergesort: Illustration
24
45
63
85
17
31
50
96
258
Mergesort: Illustration
24
45
63
85
17
31
50
96
259
Mergesort: Illustration
17
24
45
63
85
31
50
96
260
Mergesort: Illustration
17
24
45
63
85
31
50
96
261
Mergesort: Illustration
17
24
45
63
85
31
50
96
262
Mergesort: Illustration
17
24
45
63
85
31
50
96
263
Mergesort: Illustration
17
24
45
31
63
85
50
96
264
Mergesort: Illustration
17
24
45
31
63
85
50
96
265
Mergesort: Illustration
17
24
31
63
45
85
50
96
266
Mergesort: Illustration
17
24
31
63
45
85
50
96
267
Mergesort: Illustration
17
24
31
63
45
50
85
96
268
Mergesort: Illustration
17
24
31
63
45
50
85
96
269
Mergesort: Illustration
17
24
31
45
50
63
85
96
270
Mergesort: Illustration
17
24
31
45
50
63
85
96
271
Mergesort: Illustration
17
24
31
45
50
63
85
96
272
Mergesort: Illustration
17
24
31
45
50
63
85
96
273
Mergesort: Illustration
17
24
31
45
50
63
85
96
274
Mergesort: Illustration
17
24
31
45
50
63
85
96
275
Mergesort: Illustration
17
24
31
45
50
63
85
96
276
277
Method mergeSort()
void mergeSort(int a [], int left, int right)
{
// sort a[left:right]
if (left < right)
{// at least two elements
int mid = (left+right)/2;
//midpoint
mergeSort(a, left, mid);
mergeSort(a, mid + 1, right);
merge(a, b, left, mid, right); //merge from a to b
copy(b, a, left, right); //copy result back to a
}
}
Apr 18, 2012
278
void merge (int a[], int b [], int left, int mid, int right) {
int i, j, k = 0;
for (i = left, j = mid + 1; i <= mid && j <= right;) {
if (a [i] < a [j]) {
b [k] = a [i];
i++;
k++;
}
else {
b [k] = a [j];
j++;
k++;
}
}
for (; i <= mid; i++)
b [k++] = a [i];
for (; j <= right; j++)
b [k++] = a [j];
}
Apr 18, 2012
279
void copy (int b [], int a [], int left, int right) {
for (int i = 0, j = left; i <= right - left; ++i, j++)
a [j] = b [i];
}
280
AVL tree
self-adjusting tree
AVL tree
discovers: Adelson-Velskii and Landis
balanced Binary search tree
the depth of the tree: O(lg N)
definition: An AVL tree is a binary search tree with the
additional balance property that, for any node in the tree,
the height of the left and right subtrees can be differ by at
most 1.
282
283
Insertion
case 1: an insertion into the left subtree of the
child of X
case 2: an insertion into the right subtree of
left child of X
case 3: an insertion into the left subtree of
right child of X
case 4: an insertion into the right subtree of
right child of X
left
the
the
the
284
285
+2
+1 K1
K1 0
single rotation
K2 0
C
B
A
Apr 18, 2012
286
case 1
k2
12
8
12
k1 4 c 10 14
A 2
k1 4
16
6B
16
A 2 K2 8 14
1
6
B
10
C
1
Insert 1
Apr 18, 2012
287
double rotate binary tree node: first left child with its right child;
then, node k3 with new left child.
Node* doubleWithLeftChild(Node* k3) {
k3 -> left=withRightChild(k3 -> left);
return withLeftChild(k3);
}
288
K3
+2
K3
K1 -1
K2
K2
K2
K1
D
A
A
B
K3
K1
D
B
Apr 18, 2012
289
12
k1 4 D10 14
A 2
B 5
K2 6
16
k2
16
k1 4 K3 8 14
2
A
10
B C D
Insert 5
Apr 18, 2012
290
double rotate binary tree node: first right child with its left
child; then, node k1 with new right child.
Node* doubleWithRightChild (Node* k1) {
k1 -> right=withLeftChild(k1 -> right);
return withRightChild(k1);
}
291
case 3
K1 -2
K1
K2
K3 +1
K2
K3
K1
K3
A
D
K2
B
C
D
Apr 18, 2012
292
R-L type
80
0
70
-2
95
+1/-1 90
80
+1
70
99
85 92
90
90
85
80
95
92
99
95
70 85 92
Insert 85 or 92
Apr 18, 2012
293
99
294
-1 K2
K2 0
single rotation
K1
A
B
C
Apr 18, 2012
295
R-R type
80
k1
95
70
k2
80
73
95
73
70
76
76
Insert 76
Apr 18, 2012
296
Bucket Sort
Given that, we have N integers in the range 0 to
M-1
Maintain an array count of size M, which is
initialized to zero. Thus, count has M cells
(buckets).
Read Ai
Increment count[Ai] by 1
After all the input array is read, scan the count
array, printing out a representation of sorted list.
297
Radix Sort
Radix sort is generalization of bucket sort.
It uses several passes of bucket sort
Perform the bucket sorts by least
significant digits
First sort by digit in units place
Second sort by digit in tens place
Third sort by digit in hundreds place
.
298
64
1 343 125
299
Pass 1
300
0
1
2
3
4
5
6
7
8
9
Apr 18, 2012
301
64
1 343 125
0
1
2
3
4
5
6
7
8
9
Apr 18, 2012
302
64
1 343 125
0
1
2
3
4
5
6
7
8
9
Apr 18, 2012
303
64
1 343 125
0
1
2
3
4
64
5
6
7
8
9
Apr 18, 2012
304
64
1 343 125
0
1
2
3
4
64
5
6
7
8
9
Apr 18, 2012
305
64
1 343 125
0
1
2
3
4
64
5
6
7
8
9
Apr 18, 2012
306
64
1 343 125
0
1
2
3
4
64
5
6
7
8
9
Apr 18, 2012
307
64
1 343 125
0
1
2
3
4
64
5
6
216
7
8
9
Apr 18, 2012
308
64
1 343 125
0
1
2
3
4
64
5
6
216
7
8
9
Apr 18, 2012
309
64
1 343 125
0
1
2
512
3
4
64
5
6
216
7
8
9
Apr 18, 2012
310
64
1 343 125
0
1
2
512
3
4
64
5
6
216
7
8
9
Apr 18, 2012
311
64
1 343 125
0
1
2
512
3
4
64
5
6
216
27
9
Apr 18, 2012
312
64
1 343 125
0
1
2
512
3
4
64
5
6
216
27
9
Apr 18, 2012
313
64
1 343 125
0
1
2
512
3
4
64
5
6
216
27
729
Apr 18, 2012
314
64
1 343 125
0
1
2
512
3
4
64
5
6
216
27
729
Apr 18, 2012
315
64
0
1 343 125
1
2
512
3
4
64
5
6
216
27
729
Apr 18, 2012
316
64
0
1 343 125
1
2
512
3
4
64
5
6
216
27
729
Apr 18, 2012
317
64
0
512
1 343 125
3
4
64
5
6
216
27
729
Apr 18, 2012
318
64
0
512
1 343 125
3
4
64
5
6
216
27
729
Apr 18, 2012
319
64
0
512
343
64
1 343 125
5
6
216
27
729
Apr 18, 2012
320
64
0
512
343
64
1 343 125
5
6
216
27
729
Apr 18, 2012
321
64
0
512
343
64
125
216
27
729
1 343 125
322
64
0
512
343
64
125
216
27
729
1 343 125
323
512
343
64
125
216
27
729
Apr 18, 2012
324
512
343
64
125
216
27
729
Apr 18, 2012
325
0
0
512
343
64
125
216
27
729
Apr 18, 2012
326
0
0
1
512
343
64
125
216
27
729
Apr 18, 2012
327
0
0
1
512
343
64
125
216
27
729
Apr 18, 2012
328
0
1
512
343
64
125
216
27
729
Apr 18, 2012
329
0
1
2
512
343
64
125
216
27
729
Apr 18, 2012
330
1 512
0
1
2
512
343
64
125
216
27
729
Apr 18, 2012
331
1 512
0
1
2
3
343
64
125
216
27
729
Apr 18, 2012
332
Pass 2
333
8 729
0
1
2
3
4
5
6
7
8
9
Apr 18, 2012
334
8 729
0
1
2
3
4
5
6
7
8
9
Apr 18, 2012
335
0
0
8 729
1
2
3
4
5
6
7
8
9
Apr 18, 2012
336
0
0
8 729
1
2
3
4
5
6
7
8
9
Apr 18, 2012
337
0
0
8 729
1
2
3
4
5
6
7
8
9
Apr 18, 2012
338
0
0
8 729
1
2
3
4
5
6
7
8
9
Apr 18, 2012
339
0
0
512
8 729
2
3
4
5
6
7
8
9
Apr 18, 2012
340
0
0
512
8 729
2
3
4
5
6
7
8
9
Apr 18, 2012
341
0
0
512
8 729
2
3
4
343
5
6
7
8
9
Apr 18, 2012
342
0
0
512
8 729
2
3
4
343
5
6
7
8
9
Apr 18, 2012
343
0
0
512
8 729
2
3
4
343
5
6
64
7
8
9
Apr 18, 2012
344
0
0
512
8 729
2
3
4
343
5
6
64
7
8
9
Apr 18, 2012
345
0
0
512
125
8 729
3
4
343
5
6
64
7
8
9
Apr 18, 2012
346
0
0
512
125
8 729
3
4
343
5
6
64
7
8
9
Apr 18, 2012
347
0
0
512
216
125
8 729
3
4
343
5
6
64
7
8
9
Apr 18, 2012
348
0
0
512
216
125
8 729
3
4
343
5
6
64
7
8
9
Apr 18, 2012
349
0
0
512
216
125
27
8 729
3
4
343
5
6
64
7
8
9
Apr 18, 2012
350
0
0
512
216
125
27
3
4
343
5
6
64
7
8
9
Apr 18, 2012
351
0
0
512
216
125
27
8 729
3
4
343
5
6
64
7
8
9
Apr 18, 2012
352
0
0
512
216
125
27
8 729
3
4
343
5
6
64
7
8
9
Apr 18, 2012
353
0
0
512
216
125
27
8 729
729
3
4
343
5
6
64
7
8
9
Apr 18, 2012
354
0
0
512
216
125
27
8 729
729
3
4
343
5
6
64
7
8
9
Apr 18, 2012
355
512
216
125
27
729
3
4
343
5
6
64
7
8
9
Apr 18, 2012
356
0
0
512
216
125
27
729
3
4
343
5
6
64
7
8
9
Apr 18, 2012
357
0
1
512
216
125
27
729
3
4
343
5
6
64
7
8
9
Apr 18, 2012
358
8 512 216
0
1
512
216
125
27
729
3
4
343
5
6
64
7
8
9
Apr 18, 2012
359
8 512 216
0
1
2
125
27
729
3
4
343
5
6
64
7
8
9
Apr 18, 2012
360
0
1
2
125
27
729
3
4
343
5
6
64
7
8
9
Apr 18, 2012
361
0
1
2
3
4
343
5
6
64
7
8
9
Apr 18, 2012
362
0
1
2
3
4
343
5
6
64
7
8
9
Apr 18, 2012
363
0
1
2
3
4
5
6
64
7
8
9
Apr 18, 2012
364
0
1
2
3
4
5
6
64
7
8
9
Apr 18, 2012
365
Pass 3
366
0
1
2
3
4
5
6
7
8
9
Apr 18, 2012
367
000 1
0
1
2
3
4
5
6
7
8
9
Apr 18, 2012
368
0
0
1
2
3
4
5
6
7
8
9
Apr 18, 2012
369
1
2
3
4
5
6
7
8
9
Apr 18, 2012
370
0
0
1
2
3
4
5
6
7
8
9
Apr 18, 2012
371
0
0
1
2
3
4
5
6
7
8
9
Apr 18, 2012
372
0
0
1
2
3
4
5
6
7
8
9
Apr 18, 2012
373
0
0
1
2
3
4
5
6
7
8
9
Apr 18, 2012
374
0
0
1
2
3
4
5
512
6
7
8
9
Apr 18, 2012
375
0
0
1
2
3
4
5
512
6
7
8
9
Apr 18, 2012
376
0
0
1
2
216
3
4
5
512
6
7
8
9
Apr 18, 2012
377
0
0
1
2
216
3
4
5
512
6
7
8
9
Apr 18, 2012
378
0
0
125
216
3
4
5
512
6
7
8
9
Apr 18, 2012
379
0
0
125
216
3
4
5
512
6
7
8
9
Apr 18, 2012
380
0
0
125
216
27
3
4
5
512
6
7
8
9
Apr 18, 2012
381
0
0
125
216
27
3
4
5
512
6
7
8
9
Apr 18, 2012
382
0
0
125
216
27
3
4
5
512
6
7
729
8
9
Apr 18, 2012
383
0
0
125
216
27
3
4
5
512
6
7
729
8
9
Apr 18, 2012
384
0
0
125
216
343
27
4
5
512
6
7
729
8
9
Apr 18, 2012
385
0
0
125
216
343
27
4
5
512
6
7
729
8
9
Apr 18, 2012
386
0
0
125
216
343
1
27
4
5
512
6
7
729
8
9
Apr 18, 2012
387
0
0
125
216
343
1
27
4
5
512
6
7
729
8
9
Apr 18, 2012
388
125
216
343
27
64
4
5
512
6
7
729
8
9
Apr 18, 2012
389
0
0
125
216
343
1
27
27 64
64
4
5
512
6
7
729
8
9
Apr 18, 2012
390
27 64
0
1
125
216
343
4
5
512
6
7
729
8
9
Apr 18, 2012
391
27 64 125
0
1
125
216
343
4
5
512
6
7
729
8
9
Apr 18, 2012
392
27 64 125
0
1
2
216
343
4
5
512
6
7
729
8
9
Apr 18, 2012
393
27 64 125 216
0
1
2
216
343
4
5
512
6
7
729
8
9
Apr 18, 2012
394
27 64 125 216
0
1
2
3
343
4
5
512
6
7
729
8
9
Apr 18, 2012
395
0
1
2
3
343
4
5
512
6
7
729
8
9
Apr 18, 2012
396
0
1
2
3
4
5
512
6
7
729
8
9
Apr 18, 2012
397
0
1
2
3
4
5
512
6
7
729
8
9
Apr 18, 2012
398
0
1
2
3
4
5
6
7
729
8
9
Apr 18, 2012
399
0
1
2
3
4
5
6
7
729
8
9
Apr 18, 2012
400
0
1
2
3
4
5
6
7
8
9
Apr 18, 2012
401
The End
402