Ch5: Merge Sort - ecpe.nu.ac.th · Merge Sort vsCards • We have two sorted piles of a card deck....
Transcript of Ch5: Merge Sort - ecpe.nu.ac.th · Merge Sort vsCards • We have two sorted piles of a card deck....
![Page 1: Ch5: Merge Sort - ecpe.nu.ac.th · Merge Sort vsCards • We have two sorted piles of a card deck. Sorted Sorted pills pills](https://reader030.fdocuments.us/reader030/viewer/2022041207/5d63f7e588c993ba628b7e21/html5/thumbnails/1.jpg)
Ch5: Merge Sort
305233, 305234
Algorithm Analysis and Design
Jiraporn PooksookNaresuan University
![Page 2: Ch5: Merge Sort - ecpe.nu.ac.th · Merge Sort vsCards • We have two sorted piles of a card deck. Sorted Sorted pills pills](https://reader030.fdocuments.us/reader030/viewer/2022041207/5d63f7e588c993ba628b7e21/html5/thumbnails/2.jpg)
Merge Sort vs Cards
This photo is taken from https://www.pleasanttimes.com/images/2card-box-teak-2.jpg
![Page 3: Ch5: Merge Sort - ecpe.nu.ac.th · Merge Sort vsCards • We have two sorted piles of a card deck. Sorted Sorted pills pills](https://reader030.fdocuments.us/reader030/viewer/2022041207/5d63f7e588c993ba628b7e21/html5/thumbnails/3.jpg)
Merge Sort vs Cards
• We have two sorted piles of a card deck.
Sorted Sorted
pillsSorted
pills pills
![Page 4: Ch5: Merge Sort - ecpe.nu.ac.th · Merge Sort vsCards • We have two sorted piles of a card deck. Sorted Sorted pills pills](https://reader030.fdocuments.us/reader030/viewer/2022041207/5d63f7e588c993ba628b7e21/html5/thumbnails/4.jpg)
Merge Sort vs Cards
• We compare the top card of both piles and
put the smaller card on another pile.
Sorted
piles
Sorted
pilespiles piles
<
![Page 5: Ch5: Merge Sort - ecpe.nu.ac.th · Merge Sort vsCards • We have two sorted piles of a card deck. Sorted Sorted pills pills](https://reader030.fdocuments.us/reader030/viewer/2022041207/5d63f7e588c993ba628b7e21/html5/thumbnails/5.jpg)
Merge Sort vs Cards
• We compare the top card of both piles and
put the smaller card on another pile. Then we
compare the next cards.
Sorted Output Sorted
piles
Sorted
piles
Output
piles
![Page 6: Ch5: Merge Sort - ecpe.nu.ac.th · Merge Sort vsCards • We have two sorted piles of a card deck. Sorted Sorted pills pills](https://reader030.fdocuments.us/reader030/viewer/2022041207/5d63f7e588c993ba628b7e21/html5/thumbnails/6.jpg)
Merge Sort
2 4 175 3 2 6
divide
2 4 175 3 2 62 4 175 3 2 6
2 4 75 1 3 2 6
2 4 75 1 3 2 6
divide
divide
![Page 7: Ch5: Merge Sort - ecpe.nu.ac.th · Merge Sort vsCards • We have two sorted piles of a card deck. Sorted Sorted pills pills](https://reader030.fdocuments.us/reader030/viewer/2022041207/5d63f7e588c993ba628b7e21/html5/thumbnails/7.jpg)
Merge Sort
2 2 431 5 6 7
4 5 172 2 3 6
merge
4 5 172 2 3 6
5 4 72 1 3 2 6
2 4 75 1 3 2 6
merge
merge
![Page 8: Ch5: Merge Sort - ecpe.nu.ac.th · Merge Sort vsCards • We have two sorted piles of a card deck. Sorted Sorted pills pills](https://reader030.fdocuments.us/reader030/viewer/2022041207/5d63f7e588c993ba628b7e21/html5/thumbnails/8.jpg)
Merge Sort
• Divide the n element arrays to be two arrays
of size n/2
• Sort the two divided arrays using merge sort
recursivelyrecursively
• Merge the two sorted arrays to produce the
sorted answer
![Page 9: Ch5: Merge Sort - ecpe.nu.ac.th · Merge Sort vsCards • We have two sorted piles of a card deck. Sorted Sorted pills pills](https://reader030.fdocuments.us/reader030/viewer/2022041207/5d63f7e588c993ba628b7e21/html5/thumbnails/9.jpg)
Psudocode: merge(A,p,q,r)N1 = q – p + 1
N2 = r – q
Create arrays L[1..N1+1] and R[1…N2+1]
For i = 1 to N1
do L[i] = A[ p + i -1]
For j = 1 to N2
do R[j] = A[q + j]do R[j] = A[q + j]
L[N1+1] =
R[N2+1] =
i = 1
j = 1
For k=p to r
do if L[ i ] <= R[ j ]
then A[k] = L[ i ]
i = i+1
else A[k] = R[ j ]
j = j+1
∞
∞
![Page 10: Ch5: Merge Sort - ecpe.nu.ac.th · Merge Sort vsCards • We have two sorted piles of a card deck. Sorted Sorted pills pills](https://reader030.fdocuments.us/reader030/viewer/2022041207/5d63f7e588c993ba628b7e21/html5/thumbnails/10.jpg)
Practice: Merge(A,p,q,r)
4 5 172 2 3 6… …
4 5 172 2 3 6L R
K
∞4 5 172 2 3 6L R
i j
∞ ∞
![Page 11: Ch5: Merge Sort - ecpe.nu.ac.th · Merge Sort vsCards • We have two sorted piles of a card deck. Sorted Sorted pills pills](https://reader030.fdocuments.us/reader030/viewer/2022041207/5d63f7e588c993ba628b7e21/html5/thumbnails/11.jpg)
Practice: Merge(A,p,q,r)
4 5 171 2 3 6… …
4 5 172 2 3 6L R
K
∞
1 < 2
4 5 172 2 3 6L R
i j
∞ ∞
![Page 12: Ch5: Merge Sort - ecpe.nu.ac.th · Merge Sort vsCards • We have two sorted piles of a card deck. Sorted Sorted pills pills](https://reader030.fdocuments.us/reader030/viewer/2022041207/5d63f7e588c993ba628b7e21/html5/thumbnails/12.jpg)
Practice: Merge(A,p,q,r)
4 5 171 2 3 6… …
4 5 172 2 3 6L R
i j
K
∞ ∞
i j
2 5 171 2 3 6… …
4 5 172 2 3 6L R
i j
K
∞ ∞
2 <= 2
![Page 13: Ch5: Merge Sort - ecpe.nu.ac.th · Merge Sort vsCards • We have two sorted piles of a card deck. Sorted Sorted pills pills](https://reader030.fdocuments.us/reader030/viewer/2022041207/5d63f7e588c993ba628b7e21/html5/thumbnails/13.jpg)
Practice: Merge(A,p,q,r)
2 5 171 2 3 6… …
4 5 172 2 3 6L R
i j
K
∞ ∞
i j
2 2 171 2 3 6… …
4 5 172 2 3 6L R
i j
K
∞ ∞
2 < 4
![Page 14: Ch5: Merge Sort - ecpe.nu.ac.th · Merge Sort vsCards • We have two sorted piles of a card deck. Sorted Sorted pills pills](https://reader030.fdocuments.us/reader030/viewer/2022041207/5d63f7e588c993ba628b7e21/html5/thumbnails/14.jpg)
Practice: Merge(A,p,q,r)
2 2 171 2 3 6… …
4 5 172 2 3 6L R
i j
K
∞ ∞
i j
2 2 131 2 3 6… …
4 5 172 2 3 6L R
i j
K
∞ ∞
3 < 4
![Page 15: Ch5: Merge Sort - ecpe.nu.ac.th · Merge Sort vsCards • We have two sorted piles of a card deck. Sorted Sorted pills pills](https://reader030.fdocuments.us/reader030/viewer/2022041207/5d63f7e588c993ba628b7e21/html5/thumbnails/15.jpg)
Practice: Merge(A,p,q,r)
2 2 131 2 3 6… …
4 5 172 2 3 6L R
i j
K
∞ ∞
i j
2 2 431 2 3 6… …
4 5 172 2 3 6L R
i j
K
∞ ∞
4 < 6
![Page 16: Ch5: Merge Sort - ecpe.nu.ac.th · Merge Sort vsCards • We have two sorted piles of a card deck. Sorted Sorted pills pills](https://reader030.fdocuments.us/reader030/viewer/2022041207/5d63f7e588c993ba628b7e21/html5/thumbnails/16.jpg)
Practice: Merge(A,p,q,r)
2 2 431 2 3 6… …
4 5 172 2 3 6L R
i j
K
∞ ∞
i j
2 2 431 5 3 6… …
4 5 172 2 3 6L R
i j
K
∞ ∞
5 < 6
![Page 17: Ch5: Merge Sort - ecpe.nu.ac.th · Merge Sort vsCards • We have two sorted piles of a card deck. Sorted Sorted pills pills](https://reader030.fdocuments.us/reader030/viewer/2022041207/5d63f7e588c993ba628b7e21/html5/thumbnails/17.jpg)
Practice: Merge(A,p,q,r)
2 2 431 5 3 6… …
4 5 172 2 3 6L R
i j
K
∞ ∞
i j
2 2 431 5 6 6… …
4 5 172 2 3 6L R
i j
K
∞ ∞
6 < 7
![Page 18: Ch5: Merge Sort - ecpe.nu.ac.th · Merge Sort vsCards • We have two sorted piles of a card deck. Sorted Sorted pills pills](https://reader030.fdocuments.us/reader030/viewer/2022041207/5d63f7e588c993ba628b7e21/html5/thumbnails/18.jpg)
Practice: Merge(A,p,q,r)
2 2 431 5 6 6… …
4 5 172 2 3 6L R
i j
K
∞ ∞
i j
2 2 431 5 6 7… …
4 5 172 2 3 6L R
i j
K
∞ ∞
7 < ∞
![Page 19: Ch5: Merge Sort - ecpe.nu.ac.th · Merge Sort vsCards • We have two sorted piles of a card deck. Sorted Sorted pills pills](https://reader030.fdocuments.us/reader030/viewer/2022041207/5d63f7e588c993ba628b7e21/html5/thumbnails/19.jpg)
Practice: Merge(A,p,q,r)
2 2 431 5 6 7… …
4 5 172 2 3 6L R
i j
K
∞ ∞
i j
![Page 20: Ch5: Merge Sort - ecpe.nu.ac.th · Merge Sort vsCards • We have two sorted piles of a card deck. Sorted Sorted pills pills](https://reader030.fdocuments.us/reader030/viewer/2022041207/5d63f7e588c993ba628b7e21/html5/thumbnails/20.jpg)
Psudocode: merge-sort (A,p,r)
If p < r
then q = L(p+r)/2
merge-sort(A, p, q)
merge-sort(A, q+1, r)merge-sort(A, q+1, r)
merge(A, p, q, r)
![Page 21: Ch5: Merge Sort - ecpe.nu.ac.th · Merge Sort vsCards • We have two sorted piles of a card deck. Sorted Sorted pills pills](https://reader030.fdocuments.us/reader030/viewer/2022041207/5d63f7e588c993ba628b7e21/html5/thumbnails/21.jpg)
Practice: merge-sort (A,1,8)
If p < r
then q = L(p+r)/2
round 1 , q= 4
round 2 , q= 2
merge-sort(A, p, q)merge-sort(A, p, q)
round 1 , merge-sort(A,1,4)
merge-sort(A, q+1, r)
round 1 , merge-sort(A,5,8)
merge(A, p, q, r)
round 1 , merge(A,1,4,8)
![Page 22: Ch5: Merge Sort - ecpe.nu.ac.th · Merge Sort vsCards • We have two sorted piles of a card deck. Sorted Sorted pills pills](https://reader030.fdocuments.us/reader030/viewer/2022041207/5d63f7e588c993ba628b7e21/html5/thumbnails/22.jpg)
Practice: merge-sort (A,1,4)
If p < r
then q = L(p+r)/2
round 2 , q= 2
merge-sort(A, p, q)
round 1 , merge-sort(A,1,4)round 1 , merge-sort(A,1,4)
round 2 , merge-sort(A,1,2)
merge-sort(A, q+1, r)
round 1 , merge-sort(A,5,8)
round 2, merge-sort(A,3,4)
merge(A, p, q, r)
round 1 , merge(A,1,4,8)
round 2 , merge(A,1,2,4)
![Page 23: Ch5: Merge Sort - ecpe.nu.ac.th · Merge Sort vsCards • We have two sorted piles of a card deck. Sorted Sorted pills pills](https://reader030.fdocuments.us/reader030/viewer/2022041207/5d63f7e588c993ba628b7e21/html5/thumbnails/23.jpg)
Practice: merge-sort (A,5,8)
If p < r
then q = L(p+r)/2
round 2 , q= 2
merge-sort(A, p, q)
round 1 , merge-sort(A,1,4)round 1 , merge-sort(A,1,4)
round 3 , merge-sort(A,5,6)
merge-sort(A, q+1, r)
round 1 , merge-sort(A,5,8)
round 3, merge-sort(A,7,8)
merge(A, p, q, r)
round 1 , merge(A,1,4,8)
round 3 , merge(A,5,6,8)
![Page 24: Ch5: Merge Sort - ecpe.nu.ac.th · Merge Sort vsCards • We have two sorted piles of a card deck. Sorted Sorted pills pills](https://reader030.fdocuments.us/reader030/viewer/2022041207/5d63f7e588c993ba628b7e21/html5/thumbnails/24.jpg)
Practice: merge-sort (A,1,2)
If p < r
then q = L(p+r)/2
round 3 , q= 1
merge-sort(A, p, q)
round 1 , merge-sort(A,1,4)
round 2 , merge-sort(A,1,2)
round 3, merge-sort(A,1,1)round 3, merge-sort(A,1,1)
merge-sort(A, q+1, r)
round 1 , merge-sort(A,5,8)
round 2, merge-sort(A,3,4)
round 3, merge-sort(A,2,2)
merge(A, p, q, r)
round 1 , merge(A,1,4,8)
round 2 , merge(A,1,2,4)
round 3, merge(A,1,1,2)
![Page 25: Ch5: Merge Sort - ecpe.nu.ac.th · Merge Sort vsCards • We have two sorted piles of a card deck. Sorted Sorted pills pills](https://reader030.fdocuments.us/reader030/viewer/2022041207/5d63f7e588c993ba628b7e21/html5/thumbnails/25.jpg)
Practice: merge-sort (A,3,4)
If p < r
then q = L(p+r)/2
round 3 , q= 3
merge-sort(A, p, q)
round 1 , merge-sort(A,1,4)
round 2 , merge-sort(A,1,2)
round 3, merge-sort(A,3,3)round 3, merge-sort(A,3,3)
merge-sort(A, q+1, r)
round 1 , merge-sort(A,5,8)
round 2, merge-sort(A,3,4)
round 3, merge-sort(A,4,4)
merge(A, p, q, r)
round 1 , merge(A,1,4,8)
round 2 , merge(A,1,2,4)
round 3, merge(A,3,3,4)
![Page 26: Ch5: Merge Sort - ecpe.nu.ac.th · Merge Sort vsCards • We have two sorted piles of a card deck. Sorted Sorted pills pills](https://reader030.fdocuments.us/reader030/viewer/2022041207/5d63f7e588c993ba628b7e21/html5/thumbnails/26.jpg)
Practice: Merge-sort(A,p,r)
2 4 175 3 2 6
2 4 175 3 2 6L R
Merge-sort(A,1,8)
2 4 175 3 2 6L R
2 4 75
Merge-
sort(A,1,4)
1 3 2 6
Merge-
sort(A,5,8)
2 4 75 1 3 2 6
Merge-
sort(A,1,2)
Merge-
sort(A,3,4)
Merge-
sort(A,5,6)
Merge-
sort(A,7,8)
![Page 27: Ch5: Merge Sort - ecpe.nu.ac.th · Merge Sort vsCards • We have two sorted piles of a card deck. Sorted Sorted pills pills](https://reader030.fdocuments.us/reader030/viewer/2022041207/5d63f7e588c993ba628b7e21/html5/thumbnails/27.jpg)
Practice: Merge-sort(A,p,r)
2 4 175 3 2 6
2 4 175 3 2 6L R
Merge-sort(A,1,8) Merge
(A,1,4,8)
2 4 175 3 2 6L R
2 4 75
Merge-
sort(A,1,4)
1 3 2 6
Merge-
sort(A,5,8)
2 4 75 1 3 2 6
Merge-
sort(A,1,2)
Merge-
sort(A,3,4)
Merge-
sort(A,5,6)
Merge-
sort(A,7,8)
Merge
(A,1,2,4)
Merge
(A,5,6,8)
Merge
(A,1,1,2)
Merge
(A,3,3,4)
Merge
(A,5,5,6)Merge
(A,7,7,8)
![Page 28: Ch5: Merge Sort - ecpe.nu.ac.th · Merge Sort vsCards • We have two sorted piles of a card deck. Sorted Sorted pills pills](https://reader030.fdocuments.us/reader030/viewer/2022041207/5d63f7e588c993ba628b7e21/html5/thumbnails/28.jpg)
Exercise merge-sort
Input = [9,5,7,4,2,8]