Docsity

Merge Sort Algorithm: A Divide-and-Conquer Approach to Sorting, Summaries of Design

Toggle sidebar
bg1
Lists and Iterators 10/8/2019
1
1
Merge Sort &
Quick Sort
Presentation for use with the textbook, Algorithm Design and
Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015
2
Divide-and-Conquer
Divide-and conquer is a
general algorithm design
paradigm:
Divide: divide the input data
Sin two disjoint subsets S1
and S2
Conquer:
Recur: solve the
subproblems associated
with S1and S2
Combine: make the
solutions for S1and S2
into a solution for S
The base case for the
recursion are subproblems of
size 0 or 1
1
2
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37

Partial preview of the text

Download Merge Sort Algorithm: A Divide-and-Conquer Approach to Sorting and more Summaries Design in PDF only on Docsity!

1

Merge Sort &

Quick Sort

Presentation for use with the textbook, Algorithm Design andApplications, by M. T. Goodrich and R. Tamassia, Wiley, 2015

2

Divide-and-Conquer

 Divide-and conquer is ageneral algorithm design

paradigm:  Divide: divide the input data

S and in two disjoint subsets S 2 S 1  Conquer:  Recur: solve the subproblems associatedwith S 1 and S 2  Combine: make thesolutions for into a solution for S 1 and S S 2

 The base case for therecursion are subproblems of

size 0 or 1

1

2

Merge Sort

  Classical example of divide-and-conquer techniqueProblem: Givenn elements, sort elements into non-

 decreasing orderDivide-and-Conquer:  If n=1 terminate (every one-element list is alreadysorted)  If n>1, partition elements into two or more sub-collections; sort each; combine into a single sorted  How do we partition?list

Partitioning - Choice 1

  First n-1 elements into set A, last element into set BSort A using this partitioning scheme recursively  Combine A and B using method Insert() (= insertion^ ^ B already sorted  into sorted array)Leads to recursive version of InsertionSort()  Number of comparisons: O(n  Best case = n-1^2 )

 Worst case = c  i  n 2 i  n (^ n 2 ^1 )

3

4

7

Merge-Sort

 Merge-sort is a sorting algorithm based on the divide-and-conquerparadigm

 Like heap-sort  It has O ( n log n ) running time

 Unlike heap-sort  It usually needs extra space in the merging process

 It accesses data in a sequential manner (suitable to sort data on a disk)

Divide part Conquer part

Merging

 The key to Merge Sort is merging two sorted lists intoone, such that if you have two sorted lists X = (xZ = (z 1 x 2  1 …z 2 xm… ) and Y= (yz (^) m+n) 1 y 2 …y (^) n), the resulting list is  Example:L merge(L^1 = { 3 8 9 } 1 , L 2 ) = { 1 3 5 7 8 9 }^ L^2 = { 1 5 7 }

7

8

Merging

X: 3 10 23 54 Y: 1 5 25 75

Result:

Merging (cont.)

X: Y: Result:

9

10

Merging (cont.)

X: Y: Result:

Merging (cont.)

X: Y: Result:

13

14

Merging (cont.)

X: Y: Result:

Merging (cont.)

X: Y: Result:

15

16

Implementing Merge Sort

 There are two basic ways to implement merge sort:  In Place: Merging is done with only the input array   Pro: Requires only the space needed to hold the arrayCon: Takes longer to merge because if the next element is in the right side then all of the elementsmust be moved down.  Double Storage: Merging is done with a temporary arrayof the same size as the input array.  Pro: Faster than In Place since the temp array holdsthe resulting array until both left and right sides are merged into the temp array, then the temp array isappended over the input array.  Con: The memory requirement is doubled.

20

Merge-Sort Tree

 An execution of merge-sort is depicted by a binary tree  each node represents a recursive call of merge-sort and stores   unsorted sequence before the execution and its partitionsorted sequence at the end of the execution

  the root is the initial callthe leaves are calls on subsequences of size 0 or 1

7 2  9 4  2 4 7 9 7  2  2 7 9  4  4 9 7  7 2  2 9  9 4  4

19

20

21

Execution Example

 Partition

22

Execution Example (cont.)

 Recursive call, partition

21

22

25

Execution Example (cont.)

 Recursive call, base case

26

Execution Example (cont.)

 Merge

25

26

27

Execution Example (cont.)

 Recursive call, …, base case, merge

28

Execution Example (cont.)

 Merge

27

28

31

The Merge-Sort Algorithm

 Merge-sort on an inputsequence S with n elements consists ofthree steps:

 Divide: partitiontwo sequences S S 1 andinto S 2

of abouteach n  2 elements

 Recur: recursively sort and S 2 S 1

 Combine: merge S 2 into a unique sorted S 1 and

sequence

Algorithm Input sequence mergeSort S with( S ) n

Output^ elements sequence S sorted

if S.size according to() > 1 C

( mergeSortS 1 , S 2 )  ( partitionS 1 ) ( S , n /2)

mergeSort S  merge ( S ( S 2 ) 1 , S 2 )

32

Analysis of Merge-Sort

 The height  at each recursive call we divide in half the sequence, h of the merge-sort tree is O (log n )

 The overall amount or work done at  we partition and merge 2 i sequences of size all the nodes of depth n  2 i i is O ( n )

 Thus, the total running time of merge-sort is^ ^ we make^2 i ^1 recursive calls O ( n log n )

depth #seqs 0 1 size n 1 2 n  2 i 2 i^ n  2 i … … …

31

32

Evaluation

 Recurrence equation: Assume n is apower of 2

c 1 if n=

T(n) =

2T(n/2) + c 2 n if n>1, n=2k

Solution

By Substitution:T(n) = 2T(n/2) + c 2 n

T(n/2) = 2T(n/4) + c… 2 n/ T(n) = 4T(n/4) + 2 cT(n) = 8T(n/8) + 3 c 22 n n …T(n) = 2i (^) T(n/2 i (^) ) + ic 2 n Assuming n = 2side; this happens when i=kk^ , expansion halts when we get T(1) on right T(n) = 2k (^) T(1) + kc 2 n Since 2T(n) = c k^ =n, we know k=log(n); since T(1) = c 1 n + c 2 nlogn; 1 , we get thus an upper bound for TmergeSort (n) is O(nlogn)

33

34

External Sorting

 Using secondary storage effectively

 General Wisdom :

 I/O costs dominate

 Design algorithms to reduce I/O

2-Way Sort: Requires 3 Buffers

Phase 1: PREPARE.  Read a page, sort it, write it.

 only one buffer page is used

Phase 2, 3, …, etc.: MERGE:  Three buffer pages used.

Main memory buffers

INPUT 1 INPUT 2 OUTPUT Disk Disk

Disk

input Main memory Disk

1 buffer 1 buffer 1 buffer

37

38

Two-Way External Merge Sort

 Idea: conquer: Divide and sort

subfiles and mergeinto larger sorts

 Run: a list of sorteditems

Input file 1-page runs 2-page runs 4-page runs

8-page runs

PASS 0 PASS 1 PASS 2 PASS 3

9

3,4 6,2^ 9,4 8,7 5,6 3,1 2 3,4 2,6 4,9 7,8 5,6 1,3^2 2,34,6 4,78,9 5,61,3 2 2,34, 6,78,9 1,23,5 6 1,22, 3,44, 6,67,

Pass 0memory block is needed  Only one

Pass i > 0memory blocks are needed  Only three

Two-Way External Merge Sort

 Costs for one pass:read/write all pages  (^) height of tree# of passes :  Total cost :product of above

Input file 1-page runs 2-page runs 4-page runs

8-page runs

PASS 0 PASS 1 PASS 2 PASS 3

9

3,4 6,2^ 9,4 8,7 5,6 3,1 2 3,4 2,6 4,9 7,8 5,6 1,3^2 2,34,6 4,78,9 5,61,3 2 2,34, 6,78,9 1,23,5 6 1,22, 3,44,

Notice: We ignored theCPU cost to sort a block inmemory or merge two 6,67,

blocks

39

40