1
Merge Sort
2
3
4
Merging (cont.)
X:
Result:
3
10
23
54
Y:
1
5
25
75
Merging (cont.)
X:
Result:
3
10
1
23
54
Y:
5
25
75
Merging (cont.)
X:
Result:
10
1
23
3
54
Y:
5
25
75
Merging (cont.)
X:
Result:
10
1
23
3
54
5
Y:
25
75
Merging (cont.)
X:
Result:
23
1
3
54
5
Y:
10
25
75
Merging (cont.)
X:
Result:
54
1
3
5
Y:
10
25
23
75
Merging (cont.)
X:
Result:
54
1
3
5
Y:
10
75
23
25
Merging (cont.)
X:
Result:
Y:
1
3
5
10
75
23
25
54
Merging (cont.)
X:
Result:
Y:
1
3
5
10
23
25
54
75
Merge Sort Example
99
6
86 15 58 35 86
4
0
Merge Sort Example
99
99
6
6
86 15 58 35 86
86 15
58 35 86
4
0
4
0
Merge Sort Example
99
99
99
6
6
6
86 15 58 35 86
86 15
58 35 86
86 15
58 35
4
0
4
0
86
4
0
Merge Sort Example
99
99
99
99
6
6
6
6
86 15 58 35 86
86 15
58 35 86
86 15
86
15
4
0
4
0
58 35
86
4
58
86
35
0
4
0
Merge Sort Example
99
99
99
99
6
6
6
6
86 15 58 35 86
86 15
58 35 86
86 15
86
15
4
0
4
0
58 35
86
4
58
86
35
0
4
4
0
0
Merge Sort Example
99
Merge
6
86
15
58
35
86
0
4
4
0
Merge Sort Example
6
99
Merge
99
6
15 86
86
15
58 35
0
58
86
35
4
86
0
4
Merge Sort Example
6
6
Merge
99
15 86 99
15 86
0
4
58 35
35 58 86
0
4
86
Merge Sort Example
0
6
Merge
4
6
15 35 58 86 86 99
15 86 99
0
4
35 58 86
Merge Sort Example
0
4
6
15 35 58 86 86 99
24
T(n)
T(n/2)
=
n
T(n/2)
=
T(n)
n
n/2
T(n/4)
n/2
T(n/4)
T(n/4)
T(n/4)
n
=
T(n)
n/2
n/2
n/4
n/4
T(n/8)
T(n/8)
T(n/8)
n/4
n/4
T(n/8)
T(n/8)
T(n/8)
T(n/8)
T(n/8)
n
=
T(n)
n/2
n/2
n/4
n/4
n/8
n/8
1111111111111111111
n/4
n/4
1111111111111111111
n/4
n/4
n/4
n/4
1111111111111111111
n/4
n/4
1111111111111111111
n
0
n/2
1
2
n/4
n/8
i
n/8
n/2
n/4
n/8
n/4
n/8
n/8
n/4
n/8
n/8
n/8
Level i is the sum of 2i copies of n/2i
. . . . . . . . . . . . . . . . . . . . . . . . . .
log2n
1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1
=n
=n
=n
=n
=n
n(1+1+1+1+ . . . +1) =
n(logn+1)=O(nlogn)
31
Counting Inversions
32
We can
assume that
the first rank is
1, 2, ,…n.
example
R1: g, q, p, r, m1, 2, 3, 4, 5
R2: q, p, r, g, m2, 3, 4, 1, 5
33
34
35
36
37
38
39
40
Merge and Count Process: Another
example.
1, 2, 8, 10, 11, 12;
3, 4, 5, 6, 7, 9;
4 4 4 4 4 3
# of inversions
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12.
Time for merge : O(n).
41
Number Muliplication
42
Grade School Revisited:
How To Multiply Two Numbers
2X2=5
The best way
is often far
from obvious!
Time complexity of
grade school addition
+
* * * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * *
T(n) = The amount of time grade
school addition uses to add two
n-bit numbers
* * * * * * * * * * * *
We saw that T(n) was linear.
Time complexity of
grade school multiplication
-
X ** ** ** ** ** ** ** **
********
********
********
********
n2
********
********
********
********
****************
T(n) = The amount of time grade
school multiplication uses to add
two n-bit numbers
We saw that T(n) was quadratic.
Grade School Addition: (n) time
Furthermore, it is optimal
Grade School Multiplication: (n2) time
Is there a clever algorithm to
multiply two numbers in
linear time?
Open Problem!
Is there a faster way to
multiply two numbers than
the way you learned in grade
school?
Grade School Multiplication:
time
Touching Intuition
2
(n )
Intuition: Let’s say that each
time an algorithm has to
multiply a digit from one
number with a digit from the
other number, we call that a
“touch”. It seems as if any
correct algorithm must touch at
least n2 times.
Multiplication of 2 n-bit numbers
• X= a
• Y= c
• X = a 2n/2 + b
b
d
Y = c 2n/2 + d
• XY = ac 2n + (ad+bc) 2n/2 + bd
Multiplication of 2 n-bit
numbers
b
• X= a
d
• Y= c
• XY = ac 2n + (ad+bc) 2n/2 + bd
MULT(X,Y):
If |X| = |Y| = 1 then RETURN XY
Break X into a;b and Y into c;d
RETURN
MULT(a,c) 2n + (MULT(a,d) + MULT(b,c)) 2n/2 + MULT(b,d)
Multiplying (Divide & Conquer style)
• 12345678 * 21394276
• *4276
5678*2139
5678*4276
Multiplying (Divide & Conquer style)
• 12345678 * 21394276
• 1234*2139 1234*4276
5678*2139
5678*4276
Multiplying (Divide & Conquer style)
• 12345678 * 21394276
• 1234*2139 1234*4276
5678*2139
5678*4276
Multiplying (Divide & Conquer style)
• 12345678 * 21394276
• 1234*2139 1234*4276
5678*2139
5678*4276
Multiplying (Divide & Conquer style)
• 12345678 * 21394276
• 1234*2139 1234*4276
5678*2139
5678*4276
Multiplying (Divide & Conquer style)
• 12345678 * 21394276
• 1234*2139 1234*4276
5678*2139
5678*4276
• 12*21 12*39 34*21 34*39 xxxxxxxxxxxxxxxxxxxxxxxxx
Multiplying (Divide & Conquer style)
• 12345678 * 21394276
• 1234*2139 1234*4276
5678*2139
5678*4276
• 12*21 12*39 34*21 34*39 xxxxxxxxxxxxxxxxxxxxxxxxx
Multiplying (Divide & Conquer style)
• 12345678 * 21394276
• 1234*2139 1234*4276
5678*2139
5678*4276
• 12*21 12*39 34*21 34*39 xxxxxxxxxxxxxxxxxxxxxxxxx
• 1*2 1*1 2*2 2*1 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Multiplying (Divide & Conquer style)
• 12345678 * 21394276
• 1234*2139 1234*4276
5678*2139
5678*4276
• 12*21 12*39 34*21 34*39 xxxxxxxxxxxxxxxxxxxxxxxxx
2
1
4
• xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
2
Multiplying (Divide & Conquer style)
• 12345678 * 21394276
• 1234*2139 1234*4276
5678*2139
5678*4276
• 12*21 12*39 34*21 34*39 xxxxxxxxxxxxxxxxxxxxxxxxx
2
1
4
• xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
2
• Hence: 12*21 = 2*102 + (1 + 4)101
+ 2 = 252
Multiplying (Divide & Conquer style)
• 12345678 * 21394276
• 1234*2139 1234*4276
• 252
2
1
4
5678*2139
5678*4276
12*39 34*21 34*39 xxxxxxxxxxxxxxxxxxxxxxxxx
• xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
2
• Hence: 12*21 = 2*102 + (1 + 4)101
+ 2 = 252
Multiplying (Divide & Conquer style)
• 12345678 * 21394276
• 1234*2139 1234*4276
252
468
5678*2139
• xxxxxxxxxxxxxxxxxxxxxxxxx
714
1326
5678*4276
Multiplying (Divide & Conquer style)
• 12345678 * 21394276
• 1234*2139 1234*4276
5678*2139
• xxxxxxxxxxxxxxxxxxxxxxxxx
252 468
714
1326
4
2
*10 + *10 + *102 + *1
•
5678*4276
Multiplying (Divide & Conquer style)
• 12345678 * 21394276
• 1234*2139 1234*4276
•
5678*2139
• xxxxxxxxxxxxxxxxxxxxxxxxx
252 468
714
1326
4
2
*10 + *10 + *102 + *1
= 2639526
5678*4276
Multiplying (Divide & Conquer style)
• 12345678 * 21394276
• 2639526
1234*4276
5678*2139
5678*4276
Multiplying (Divide & Conquer style)
• 12345678 * 21394276
• 2639526
5276584
12145242
24279128
Multiplying (Divide & Conquer style)
• 12345678 * 21394276
• 2639526
*108
+
5276584
*104 +
12145242
*104 +
24279128
*1
Multiplying (Divide & Conquer style)
• 12345678 * 21394276
• 2639526
*108
+
5276584
*104 +
12145242
*104 +
= 264126842539128
24279128
*1
Recurrence Relation
• T(1) = k for some constant k
• T(n) = 4 T(n/2) + k’ n + k’’ for some
constants k’ and k’’
MULT(X,Y):
If |X| = |Y| = 1 then RETURN XY
Break X into a;b and Y into c;d
RETURN
MULT(a,c) 2n + (MULT(a,d) + MULT(b,c)) 2n/2 + MULT(b,d)
Let’s be concrete to keep it
simple
• T(1) = 1
• T(n) = 4 T(n/2) + n
• Notice that T(n) is inductively defined only
for positive powers of 2
• How do we unravel T(n) so that we can
determine its growth rate?
T(n)
T(n/2)
n
=
T(n/2)
T(n/2)
T(n/2)
n
=
T(n)
n/2
T(n/4)
T(n/4)
T(n/4)
T(n/4)
T(n/2)
T(n/2)
T(n/2)
n/2
n/2
T(n/4)
T(n/4)
n
=
T(n)
T(n/4)
T(n/4)
T(n/4)
T(n/4)
n/2
T(n/4)
T(n/4)
T(n/4)
T(n/4)
n/2
T(n/4)
T(n/4)
T(n/4)
T(n/4)
T(n/4)
T(n/4)
T(n)
n
=
n/2
n/2
n/4 n/4
n/4
n/4
n/4
n/4
n/4
n/2
n/4
n/4
n/4
n/4
n/2
n/4
n/4
11111111111111111111111111111111 . . . . . . 111111111111111111111111111111111
n/4
n/4 n/4
n
0
1
2
i
n/2
+
n/2
+
n/2
+
n/2
n/4 + n/4 + n/4 + n/4 + n/4 + n/4 + n/4 + n/4 + n/4 + n/4 + n/4 + n/4 + n/4 + n/4 + n/4 + n/4
Level i is the sum of 4i copies of n/2i
. . . . . . . . . . . . . . . . . . . . . . . . . .
log2n
1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1
= 1n
= 2n
= 4n
= 2in
= (n)n
n(1+2+4+8+ . . . +n) =
n(2n-1) =
2n2-n
Divide and Conquer MULT: (n2) time
Grade School Multiplication: (n2) time
All that work for nothing!
CAUTION: Beyond this point is for interests
only (not required for this course).
80
81
82
83
84
85
86
87
88
89
90
2
(o.5 )
((0.5)2+(0.5)2)0.5=(0.52)0.5<
91
Can be done in O(n) time if we sort all the points
first.
92
93
Closest-Pair(p1, …, pn) {
xg: array sorted based on x in increasing order
yg: array sorted based on y in increasing order
Compute separation line L such that half the points are on one side and half on
the other side.
Create sorted xg1 (left half), xg2(right half), yg1(left half) and yg2 (right half). O(n)
1 = Closest-Pair(xg1,yg1) (2 = Closest-Pair(xg2,yg2)
( = min((1, 2))
Merge:
Delete all points further than from separation line L (O(n) time)
Create array new-yg for remaining points sorted by y-coordinatein (O(n) time).
for (i=0; i<= size of new-yg; i++)
for (j=1; j<=11; j++)
(the nested loop takes O(n) time)
if (d(new-yg[i], new-yg[i+j]<) then =d(new-yg[i], new-yg[j];
return .
}
94
Example 1
• Input: points p1, p2,…,p8 in a plane.
• p1=(1,4), p2=(2,5), p3=(4,2), p4=(7,2), p5=(10,3),
p6=(13,4),p7=(14,4), p8=(15,3)
6
p6
p2
4
p7
p1
p8
p5
2
p4
p3
0
2
4
6
8
10
12
14
16
95
Find a line L1 such that 4 points are on one
side and the other 4 points are on the other
side.
6
L1
p6
p2
4
p7
p1
p8
p5
2
p4
p3
0
2
4
6
8
10
12
14
16
96
Consider the left four points p1,p2,p3,p4. Find a line L2 such that 2
points are on one side and the other 2 points are on the other side.
δ1 =Closest-pair (Region 1) = dist(p1,p2) =
2.
δ2 =Closest- pair (Region 2) = dist(p3,p4) = 3
δ=min(δ1 ,δ2)= 2
6
2
4
L2
Reg 2
L1
p6
p1
p7
p2
3
Reg 1
0
2
p4
p3
4
p8
p5
2
6
8
10
12
14
16
97
• Delete the points in Region 1 and 2 further than δ=
from L2
• Compare the distance dist(p1,p3) with δ.
Here dist(p1,p3) = 13 > δ, δ is not updated.
• Closest-pair (Region 1 and 2) = dist(p1,p2) = 2 .
2
6
L2
p1
4
Reg 2
L1
p6
p7
13
p8
p5
2
Reg 1
0
2
p4
p3
4
6
8
10
12
14
16
98
• For the four points p5,p6,p7,p8, we use a line L3 to divide them
and use similar method to find
10
δ1 =Closest-pair (Region 3) = dist(p5,p6) =
δ2 =Closest- pair (Region 4) = dist(p7,p8) =
δ= min(δ1 ,δ2)=
2
2
6
2
4
L2
Reg 2
L1 Reg 3
p1
p6
L3
p7
10
p2
2
p8
p5
2
Reg 1
0
2
p4
p3
4
Reg 4
6
8
10
12
14
16
99
• Delete the points in Region 1 and 2 further than δ=
from L3
• Compare the distance dist(p6,p8) with δ.
Here dist(p6,p8) = 5 >δ, δ is not updated.
• Compare the distance dist(p6,p7) with δ.
Here dist(p6,p7) = 1 < δ, δ is updated.
• Closest-pair (Region 3 and 4) = dist(p6,p7) = 1.
2
6
2
4
L2
Reg 2
L1 Reg 3
p1
p6
L3
1 p7
Reg 4
p2
5
p5
2
Reg 1
0
2
p4
p3
4
p8
6
8
10
12
14
16
100
Consider Region 1 , 2 and Region 3, 4 as two large regions.
2
δ1 =Closest-pair (Region 1 and 2) = dist(p1,p2) =
δ2 =Closest- pair (Region 3 and 4) = dist(p6,p7) = 1
δ= min(δ1 ,δ2)= 1
6
2
4
L2
Reg 2
L1 Reg 3
p1
p6
L3
1 p7
Reg 4
p2
p8
p5
2
Reg 1
0
2
p4
p3
4
6
8
10
12
14
16
101
• Delete the points in Region 1, 2, 3 and 4 further than δ= 1
from L1
• Here only one point p4 is left, δ is not updated.
• Closest-pair (Region 1, 2, 3 and 4) = dist(p6,p7) = 1.
6
2
4
L2
Reg 2
L1 Reg 3
p1
p6
L3
1 p7
Reg 4
p2
p8
p5
2
Reg 1
0
2
p3
4
p4
6
8
10
12
14
16
102
Closest pair of points
Question: How to handle the case, where two
points can have the same x-coordinate?
103
© Copyright 2026 Paperzz