Download - 2. Getting Started Heejin Park College of Information and Communications Hanyang University
![Page 1: 2. Getting Started Heejin Park College of Information and Communications Hanyang University](https://reader036.vdocument.in/reader036/viewer/2022070305/5513fe23550346e2488b4778/html5/thumbnails/1.jpg)
2. Getting Started
Heejin ParkCollege of Information and Communications
Hanyang University
![Page 2: 2. Getting Started Heejin Park College of Information and Communications Hanyang University](https://reader036.vdocument.in/reader036/viewer/2022070305/5513fe23550346e2488b4778/html5/thumbnails/2.jpg)
2
Contents
Sorting problem
2 sorting algorithms Insertion sort Merge sort
![Page 3: 2. Getting Started Heejin Park College of Information and Communications Hanyang University](https://reader036.vdocument.in/reader036/viewer/2022070305/5513fe23550346e2488b4778/html5/thumbnails/3.jpg)
3
Sorting problem
Input A sequence of n number <a1, a2, . . ., an>.
Output A permutation (reordering) <a’1, a’2, . . ., a’n> of the input se
quence such that a’1≤ a’2≤ … ≤ a’n.
Ex> Input: < 5, 2, 4, 6, 1, 3> Output: < 1, 2, 3, 4, 5, 6>
keys
![Page 4: 2. Getting Started Heejin Park College of Information and Communications Hanyang University](https://reader036.vdocument.in/reader036/viewer/2022070305/5513fe23550346e2488b4778/html5/thumbnails/4.jpg)
4
Insertion sort
Insertion sort Description Correctness Performance
![Page 5: 2. Getting Started Heejin Park College of Information and Communications Hanyang University](https://reader036.vdocument.in/reader036/viewer/2022070305/5513fe23550346e2488b4778/html5/thumbnails/5.jpg)
5
Description
What is insertion sort? A sorting algorithm using insertion.
What is insertion? Given a key and a sorted list of keys, insert a key
into a sorted list of keys preserving the sorted order.
ex> Insert 3 into <1, 2, 4, 5, 6>
![Page 6: 2. Getting Started Heejin Park College of Information and Communications Hanyang University](https://reader036.vdocument.in/reader036/viewer/2022070305/5513fe23550346e2488b4778/html5/thumbnails/6.jpg)
6
Description
Insertion sort is an incremental approach. Let A[1..n] denote the array storing keys. Insert A[2] into A[1]. Insert A[3] into A[1..2]. Insert A[4] into A[1..3]. . . . Insert A[n] into A[1..n-1].
![Page 7: 2. Getting Started Heejin Park College of Information and Communications Hanyang University](https://reader036.vdocument.in/reader036/viewer/2022070305/5513fe23550346e2488b4778/html5/thumbnails/7.jpg)
7
Description: example
5 2 4 6 1 3
5 2 4 6 1 3
2 5 4 6 1 3
2 4 5 6 1 3
2 4 5 6 1 3
1 2 4 5 6 3
1 2 3 4 5 6
![Page 8: 2. Getting Started Heejin Park College of Information and Communications Hanyang University](https://reader036.vdocument.in/reader036/viewer/2022070305/5513fe23550346e2488b4778/html5/thumbnails/8.jpg)
8
Description: pseudo code
INSERTION-SORT(A)for j ← 2 to length[A]
do key ← A[j]i ← j - 1
while i > 0 and A[i] > keydo A[i + 1] ← A[i]
i ← i - 1A[i + 1] ← key
Insert A[j] into A[1..j - 1].n-1 iterations of insertion.
Find a place to put A[j]. Put A[j].
Pseudocode conventions are given in p. 19 - 20 of the textbook.
![Page 9: 2. Getting Started Heejin Park College of Information and Communications Hanyang University](https://reader036.vdocument.in/reader036/viewer/2022070305/5513fe23550346e2488b4778/html5/thumbnails/9.jpg)
9
Insertion sort
Insertion sort Description Correctness Performance
Running time Space consumption
![Page 10: 2. Getting Started Heejin Park College of Information and Communications Hanyang University](https://reader036.vdocument.in/reader036/viewer/2022070305/5513fe23550346e2488b4778/html5/thumbnails/10.jpg)
10
Running time
How to analyze the running time of an algorithm? Consider running the algorithm on a specific machine and
measure the running time. We cannot compare the running time of an algorithm on a machine
with the running time of another algorithm on another machine. So, we have to measure the running time of every algorithm on a
specific machine, which is impossible. Hence, we count the number of instructions used by the
algorithm.
![Page 11: 2. Getting Started Heejin Park College of Information and Communications Hanyang University](https://reader036.vdocument.in/reader036/viewer/2022070305/5513fe23550346e2488b4778/html5/thumbnails/11.jpg)
11
Instructions
Arithmetic Add, Subtract, Multiply, Divide, remainder, floor, ceiling
Data movement Load, store, copy
Control Conditional branch Unconditional branch Subroutine call and return
![Page 12: 2. Getting Started Heejin Park College of Information and Communications Hanyang University](https://reader036.vdocument.in/reader036/viewer/2022070305/5513fe23550346e2488b4778/html5/thumbnails/12.jpg)
12
Running time
The running time of an algorithm grows with the input size, which is the number of items in the input.
For example, sorting 10 keys is faster than sorting 100 keys.
So the running time of an algorithm is described as a function of input size n, for example, T(n).
![Page 13: 2. Getting Started Heejin Park College of Information and Communications Hanyang University](https://reader036.vdocument.in/reader036/viewer/2022070305/5513fe23550346e2488b4778/html5/thumbnails/13.jpg)
13
Running time of insertion sort
INSERTION-SORT(A) cost times
for j ← 2 to length[A] c1 n
do key ← A[j] c2 n - 1
i ← j - 1 c4 n - 1
while i > 0 and A[i] > key c5
do A[i + 1] ← A[i] c6
i ← i - 1 c7
A[i + 1] ← key c8 n - 1
n
j
jt2
T(n): The sum of product of cost and times of each line.
n
j
jt2
)1(
n
j
jt2
)1(
![Page 14: 2. Getting Started Heejin Park College of Information and Communications Hanyang University](https://reader036.vdocument.in/reader036/viewer/2022070305/5513fe23550346e2488b4778/html5/thumbnails/14.jpg)
14
Running time of insertion sort
INSERTION-SORT(A) cost times
for j ← 2 to length[A] c1 n
do key ← A[j] c2 n - 1
i ← j - 1 c4 n - 1
while i > 0 and A[i] > key c5
do A[i + 1] ← A[i] c6
i ← i - 1 c7
A[i + 1] ← key c8 n - 1
n
j
jt2
T(n): The sum of product of cost and times of each line.
n
j
jt2
)1(
n
j
jt2
)1(
)1()1(
)1(
)1()1()(
87
65
421
2
22
nctc
tctc
ncncncnT
n
j
n
j
n
j
j
jj
![Page 15: 2. Getting Started Heejin Park College of Information and Communications Hanyang University](https://reader036.vdocument.in/reader036/viewer/2022070305/5513fe23550346e2488b4778/html5/thumbnails/15.jpg)
15
Running time of insertion sort
ti: The number of times the while loop test is executed for j.
Note that for, while loop test is executed one time more than the loop body.
![Page 16: 2. Getting Started Heejin Park College of Information and Communications Hanyang University](https://reader036.vdocument.in/reader036/viewer/2022070305/5513fe23550346e2488b4778/html5/thumbnails/16.jpg)
16
Running time of insertion sort
Although the size of the input is the same, we have best case average case, and worst case.
)1()1(
)1()1()1()(
87
65421
2
22
nctc
tctcncncncnT
n
j
n
j
n
j
j
jj
![Page 17: 2. Getting Started Heejin Park College of Information and Communications Hanyang University](https://reader036.vdocument.in/reader036/viewer/2022070305/5513fe23550346e2488b4778/html5/thumbnails/17.jpg)
17
Running time of insertion sort
Best case If A[1..n] is already sorted, tj = 1 for j = 2, 3,…, n.
This running time can be expressed as an+b for constants a and b; it is thus a linear function of n.
)()(
)1()1()1()1(
)1()1(
)1()1()1()(
854285421
85421
87
65421
2
22
ccccnccccc
ncncncncnc
nctc
tctcncncncnT
n
j
n
j
n
j
j
jj
![Page 18: 2. Getting Started Heejin Park College of Information and Communications Hanyang University](https://reader036.vdocument.in/reader036/viewer/2022070305/5513fe23550346e2488b4778/html5/thumbnails/18.jpg)
18
12
)1(
2
nnj
n
j
Worst case If A[1..n] is sorted in reverse order, tj = j for j = 2, 3,…, n.
This running time can be expressed as an2 + bn + c for constants a, b, and c; it is thus a quadratic function of n.
and 2
)1()1(
2
nnj
n
j
)()222
()222
(
)1()2
)1(()
2
)1((
)12
)1(()1()1()(
85428765
4212765
876
5421
ccccncccc
cccnccc
ncnn
cnn
c
nncncncncnT
Running time of insertion sort
![Page 19: 2. Getting Started Heejin Park College of Information and Communications Hanyang University](https://reader036.vdocument.in/reader036/viewer/2022070305/5513fe23550346e2488b4778/html5/thumbnails/19.jpg)
19
Running time of insertion sort
Only the degree of leading term is important. Because we are only interested in the rate of growt
h or order of growth. For example, a quadratic function grows faster than
any linear function.
The degree of leading term is expressed as Θ–notation. The worst-case running time of insertion sort is Θ(n2).
![Page 20: 2. Getting Started Heejin Park College of Information and Communications Hanyang University](https://reader036.vdocument.in/reader036/viewer/2022070305/5513fe23550346e2488b4778/html5/thumbnails/20.jpg)
20
Space consumption of insertion sort
Θ(n) space.
Moreover, the input numbers are sorted in place. n + c space for some constant c.
![Page 21: 2. Getting Started Heejin Park College of Information and Communications Hanyang University](https://reader036.vdocument.in/reader036/viewer/2022070305/5513fe23550346e2488b4778/html5/thumbnails/21.jpg)
21
Content
Sorting problem
Sorting algorithms Insertion sort - Θ(n2). Merge sort - Θ(nlgn).
![Page 22: 2. Getting Started Heejin Park College of Information and Communications Hanyang University](https://reader036.vdocument.in/reader036/viewer/2022070305/5513fe23550346e2488b4778/html5/thumbnails/22.jpg)
22
Merge
What is merge sort? A sorting algorithm using merge.
What is merge? Given two sorted lists of keys, generate a sorted list
of the keys in the given sorted lists. <1, 5, 6, 8> < 2, 4, 7, 9> < 1, 2, 4, 5, 6, 7, 8, 9>
![Page 23: 2. Getting Started Heejin Park College of Information and Communications Hanyang University](https://reader036.vdocument.in/reader036/viewer/2022070305/5513fe23550346e2488b4778/html5/thumbnails/23.jpg)
23
Merge
Merging example <1, 5, 6, 8> < 2, 4, 7, 9> < 5, 6, 8> < 2, 4, 7, 9> < 5, 6, 8> < 4, 7, 9> < 5, 6, 8> < 7, 9> < 6, 8> < 7, 9> < 8> < 7, 9> < 8> < 9> < > < 9>
< 1 >
< 1, 2 >
< 1, 2, 4 >
< 1, 2, 4, 5 >
< 1, 2, 4, 5, 6 >
< 1, 2, 4, 5, 6, 7 >
< 1, 2, 4, 5, 6, 7, 8 >
< 1, 2, 4, 5, 6, 7, 8, 9>
![Page 24: 2. Getting Started Heejin Park College of Information and Communications Hanyang University](https://reader036.vdocument.in/reader036/viewer/2022070305/5513fe23550346e2488b4778/html5/thumbnails/24.jpg)
24
Merge
Running time of merge Let n1 and n2 denote the lengths of two sorted lists.
Θ(n1 + n2) time. Main operations: compare and move #comparison ≤ #movement Obviously, #movement = n1 + n2
So, #comparison ≤ n1 + n2
Hence, #comparison + #movement ≤ 2(n1 + n2)
which means Θ(n1 + n2).
![Page 25: 2. Getting Started Heejin Park College of Information and Communications Hanyang University](https://reader036.vdocument.in/reader036/viewer/2022070305/5513fe23550346e2488b4778/html5/thumbnails/25.jpg)
25
Merge sort
A divide-and-conquer approach Divide: Divide the n keys into two lists of n/2 keys. Conquer: Sort the two lists recursively using merge sort. Combine: Merge the two sorted lists.
![Page 26: 2. Getting Started Heejin Park College of Information and Communications Hanyang University](https://reader036.vdocument.in/reader036/viewer/2022070305/5513fe23550346e2488b4778/html5/thumbnails/26.jpg)
26
5 2 4 7 1 3 2 6
5 2 4 7 1 3 2 6
5 2 4 7 1 3 2 6
5 2 4 7 1 3 2 6
divide
divide divide
Merge sort
![Page 27: 2. Getting Started Heejin Park College of Information and Communications Hanyang University](https://reader036.vdocument.in/reader036/viewer/2022070305/5513fe23550346e2488b4778/html5/thumbnails/27.jpg)
27
1 2 2 3 4 5 6 7
2 4 5 7 1 2 3 6
2 5 4 7 1 3 2 6
5 2 4 7 1 3 2 6
merge
merge
merge
Merge sort
![Page 28: 2. Getting Started Heejin Park College of Information and Communications Hanyang University](https://reader036.vdocument.in/reader036/viewer/2022070305/5513fe23550346e2488b4778/html5/thumbnails/28.jpg)
28
Pseudo code
MERGE-SORT(A, p, r)
1 if p < r
2 then q ← (⌊ p + r)/2⌋3 MERGE-SORT(A, p, q)
4 MERGE-SORT(A, q + 1, r)
5 MERGE(A, p, q, r)
![Page 29: 2. Getting Started Heejin Park College of Information and Communications Hanyang University](https://reader036.vdocument.in/reader036/viewer/2022070305/5513fe23550346e2488b4778/html5/thumbnails/29.jpg)
29
Running time
Divide: Θ(1) The divide step just computes the middle of the subarray,
which takes constant time.
Conquer: 2T (n/2) We recursively solve two subproblems, each of size n/2.
Combine: Θ(n) We already showed that merging two sorted lists of size
n/2 takes Θ(n) time.
![Page 30: 2. Getting Started Heejin Park College of Information and Communications Hanyang University](https://reader036.vdocument.in/reader036/viewer/2022070305/5513fe23550346e2488b4778/html5/thumbnails/30.jpg)
30
Running time
)()2/(2
)1()(
nnTnT
if n=1,
if n >1
T(n) can be represented as a recurrence.
![Page 31: 2. Getting Started Heejin Park College of Information and Communications Hanyang University](https://reader036.vdocument.in/reader036/viewer/2022070305/5513fe23550346e2488b4778/html5/thumbnails/31.jpg)
31
Running time
where the constant c represents the time required to solve problems of size 1 as well as the time per array element of the divide and combine steps.
)()2/(2
)1()(
nnTnT
if n=1,
if n >1
cnnT
cnT
)2/(2)(
if n=1,
if n >1
![Page 32: 2. Getting Started Heejin Park College of Information and Communications Hanyang University](https://reader036.vdocument.in/reader036/viewer/2022070305/5513fe23550346e2488b4778/html5/thumbnails/32.jpg)
32
Recursion tree
T(n)cn
T(n/2) T(n/2)
T(n/4)
cn
cn/2 cn/2
T(n/4) T(n/4)T(n/4)
![Page 33: 2. Getting Started Heejin Park College of Information and Communications Hanyang University](https://reader036.vdocument.in/reader036/viewer/2022070305/5513fe23550346e2488b4778/html5/thumbnails/33.jpg)
33
Recursion tree
cn
cn/2 cn/2
cn/4 cn/4 cn/4 cn/4
c c c c c c c…
![Page 34: 2. Getting Started Heejin Park College of Information and Communications Hanyang University](https://reader036.vdocument.in/reader036/viewer/2022070305/5513fe23550346e2488b4778/html5/thumbnails/34.jpg)
34
Recursion tree
cn
cn/2 cn/2
cn/4 cn/4 cn/4 cn/4
c c c c c c c…
cn
cn
cn
cn
![Page 35: 2. Getting Started Heejin Park College of Information and Communications Hanyang University](https://reader036.vdocument.in/reader036/viewer/2022070305/5513fe23550346e2488b4778/html5/thumbnails/35.jpg)
35
Recursion tree
cn
cn/2 cn/2
cn/4 cn/4 cn/4 cn/4
c c c c c c c…
cn
cn
cn
cn
Total : cnlgn+cn = Θ (nlgn)
lgn + 1
![Page 36: 2. Getting Started Heejin Park College of Information and Communications Hanyang University](https://reader036.vdocument.in/reader036/viewer/2022070305/5513fe23550346e2488b4778/html5/thumbnails/36.jpg)
36
Divide and conquer
Suppose that our division of the problem yields a subproblems, each of which is 1/b the size of the original. We shall see many divide-and-conquer algorithms in which a ≠ b.
Let D(n) denote time to divide the problem into subproblems.
Let C(n) denote time to combine the solutions to the subproblems into the solution to the original problem.
We get the recurrence
)()()/(
)1()(
nCnDbnaTnT
if n≤c,
otherwise.
![Page 37: 2. Getting Started Heejin Park College of Information and Communications Hanyang University](https://reader036.vdocument.in/reader036/viewer/2022070305/5513fe23550346e2488b4778/html5/thumbnails/37.jpg)
37
For merge sort, a = b = 2. D(n) = Θ(1). C(n) = Θ(n).
The worst-case running time T (n) of merge sort:
)()2/(2
)1()(
nnTnT
if n=1,
if n >1
Divide and conquer
![Page 38: 2. Getting Started Heejin Park College of Information and Communications Hanyang University](https://reader036.vdocument.in/reader036/viewer/2022070305/5513fe23550346e2488b4778/html5/thumbnails/38.jpg)
38
Binary Search Exercises 2.3-5 (page 37)
Selection sort Exercises 2.2-2 (page 27)
Bubble sort Problems 2-2 (page 38)
Horner’s rule Problems 2-3 (page 39)
Loop invariant is difficult.
More (sorting) algorithms