• No results found

05. Merge Sort Divide And Conquer Approach

N/A
N/A
Protected

Academic year: 2020

Share "05. Merge Sort Divide And Conquer Approach"

Copied!
32
0
0

Loading.... (view fulltext now)

Full text

(1)

Design and Analysis of

Algorithms

(2)

Designing Algorithms

• There are many ways to design algorithms

• Insertion sort uses an incremental approach

– Having sorted to array A[i..j]

– We insert the single element A[j] into its proper place, to get a sorted array A[i..j]

• An alternative design approach

(3)

Divide

the problems into a number of sub

problems.

Conquer

the sub problems by solving

them recursively. If the sub-problem sizes

are small enough, just solve the problems

in a straight forward manner.

Combine

the solutions to the sub

problems into the solution for the original

problem.

(4)

Divide

the n element sequence to be

sorted into two subsequences of n/2

elements each.

Conquer:

Sort the two subsequences to

produce the sorted answer.

Combine:

Merge the two sorted sub

(5)

Merge Sort

Base Case: When the sequences to be sorted has length 1.

108 56 14 89 12 34 66

Unsorted

108 56 14 66 Divide 108 66 Divide Merge 56 14 Divide 14 56 Merge 56 66 108 14 Merge 12 89 34 Divide 12 89 Divide 89 12 Merge 34 Divide 34 BCase 34 Merge 34 12 89 Merge 14 34 56 66 89 108 12

(6)

Merge-sort(A, f, l)

1.

if

f < l

2.

then

m = (f + l)/2

3.

Merge-sort(A, f, m)

4.

Merge-sort(A, m + 1, l)

5. Merge(A, f, m, l)

(7)

Merge(A, f, m, l)

1. T[f..l] \\declare temporary array of same size

2. i  f; k  f; j  m + 1 \\initialize integers i, j, and k

3. while (i  m) and (j  l)

4. do if (A[i]  A[j]) \\comparison of elements

5. then T[k++]  A[i++] 6. else T[k++]  A[j++] 7. while (i  m)

8. do T[k++]  A[i++] \\copy from A to T

(8)

Merge Algorithm

• The basic merging algorithms takes

– Two input arrays, A[] and B[],

– One output array C[]

– And three counters aptr, bptr and cptr. (initially

set to the beginning of their respective arrays)

• The smaller of A[aptr] and B[bptr] is copied to the

next entry in C i.e. C[cptr].

• The appropriate counters are then advanced.

• When either of input list is exhausted, the

(9)

Merge Algorithm

56 66 108 14

A[]

aptr

34

12 89

B[]

bptr

If A[aptr] < B[bptr]

C[cptr++] = A[aptr++]

Else

C[cptr++] = B[bptr++]

14 > 12 therefore

C[]

(10)

Merge Algorithm

56 66 108 14

A[]

aptr

34

12 89

B[]

If A[aptr] < B[bptr]

C[cptr++] = A[aptr++]

Else

C[cptr++] = B[bptr++]

14 > 12 therefore

cptr++ C[]

bptr

C[cptr] = 12

(11)

Merge Algorithm

56 66 108 14

A[]

aptr

34

12 89

B[]

If A[aptr] < B[bptr]

C[cptr++] = A[aptr++]

Else

C[cptr++] = B[bptr++]

14 > 12 therefore

cptr++ C[]

bptr

bptr++

(12)

56 66 108 14

A[]

aptr

34

12 89

B[]

If A[aptr] < B[bptr]

C[cptr++] = A[aptr++]

Else

C[cptr++] = B[bptr++]

14 < 34 therefore

C[]

bptr

12

C[cptr] = 14

14

(13)

56 66 108 14

A[]

aptr

34

12 89

B[]

If A[aptr] < B[bptr]

C[cptr++] = A[aptr++]

Else

C[cptr++] = B[bptr++]

14 < 34 therefore

cptr++ C[]

bptr

C[cptr] = 14

(14)

56 66 108 14

A[]

34

12 89

B[]

If A[aptr] < B[bptr]

C[cptr++] = A[aptr++]

Else

C[cptr++] = B[bptr++]

14 < 34 therefore

cptr++ C[]

bptr aptr

aptr++

12

C[cptr] = 14

14

(15)

56 66 108 14

A[]

aptr

34

12 89

B[]

If A[aptr] < B[bptr]

C[cptr++] = A[aptr++]

Else

C[cptr++] = B[bptr++]

56 > 34 therefore

C[]

bptr

C[cptr] = 34

(16)

56 66 108 14

A[]

aptr

34

12 89

B[]

If A[aptr] < B[bptr]

C[cptr++] = A[aptr++]

Else

C[cptr++] = B[bptr++]

56 > 34 therefore

cptr++ C[]

bptr

12

C[cptr] = 34

14 34

(17)

56 66 108 14

A[]

aptr

34

12 89

B[]

If A[aptr] < B[bptr]

C[cptr++] = A[aptr++]

Else

C[cptr++] = B[bptr++]

56 > 34 therefore

cptr++ C[]

bptr

bptr++

C[cptr] = 34

(18)

56 66 108 14

A[]

aptr

34

12 89

B[]

If A[aptr] < B[bptr]

C[cptr++] = A[aptr++]

Else

C[cptr++] = B[bptr++]

56 < 89 therefore

C[]

bptr

12 14 34

C[cptr] = 56

56

(19)

56 66 108 14

A[]

aptr

34

12 89

B[]

If A[aptr] < B[bptr]

C[cptr++] = A[aptr++]

Else

C[cptr++] = B[bptr++]

56 < 89 therefore

cptr++ C[]

bptr

C[cptr] = 56

(20)

56 66 108 14

A[]

34

12 89

B[]

If A[aptr] < B[bptr]

C[cptr++] = A[aptr++]

Else

C[cptr++] = B[bptr++]

56 < 89 therefore

cptr++ C[]

bptr aptr

aptr++

12 14 34

C[cptr] = 56

56

(21)

56 66 108 14

A[]

34

12 89

B[]

If A[aptr] < B[bptr]

C[cptr++] = A[aptr++]

Else

C[cptr++] = B[bptr++]

66 < 89 therefore

C[]

bptr aptr

C[cptr] = 66

(22)

56 66 108 14

A[]

34

12 89

B[]

If A[aptr] < B[bptr]

C[cptr++] = A[aptr++]

Else

C[cptr++] = B[bptr++]

66 < 89 therefore

cptr++ C[]

bptr aptr

12 14 34

C[cptr] = 66

56 66

(23)

56 66 108 14

A[]

34

12 89

B[]

If A[aptr] < B[bptr]

C[cptr++] = A[aptr++]

Else

C[cptr++] = B[bptr++]

66 < 89 therefore

cptr++ C[]

bptr aptr

aptr++

C[cptr] = 66

(24)

56 66 108 14

A[]

34

12 89

B[]

If A[aptr] < B[bptr]

C[cptr++] = A[aptr++]

Else

C[cptr++] = B[bptr++]

108 > 89 therefore

C[]

bptr aptr

12 14 34 56 66 C[cptr] = 89

89

(25)

56 66 108 14

A[]

34

12 89

B[]

If A[aptr] < B[bptr]

C[cptr++] = A[aptr++]

Else

C[cptr++] = B[bptr++]

108 > 89 therefore

cptr++ C[]

bptr aptr

C[cptr] = 89

(26)

56 66 108 14

A[]

34

12 89

B[]

If A[aptr] < B[bptr]

C[cptr++] = A[aptr++]

Else

C[cptr++] = B[bptr++]

108 > 89 therefore

cptr++ C[]

bptr aptr

bptr++

12 14 34 56 66 C[cptr] = 89

89

(27)

56 66 108 14

A[]

34

12 89

B[]

If A[aptr] < B[bptr]

C[cptr++] = A[aptr++]

Else

C[cptr++] = B[bptr++]

Array B is now finished, copy

remaining elements of array A in array C

C[]

bptr aptr

(28)

56 66 108 14

A[]

34

12 89

B[]

If A[aptr] < B[bptr]

C[cptr++] = A[aptr++]

Else

C[cptr++] = B[bptr++]

Array B is now finished, copy

remaining elements of array A in array C

C[]

bptr aptr

12 14 34 56 66 89 108

(29)

• Let T(n) be the time taken by this algorithm to sort an array of n

elements dividing A into sub-arrays A1and A2.

• It is easy to see that the Merge (A1, A2, A) takes the linear time. Consequently,

T(n) = T(n/2) + T(n/2) + θ(n)

T(n) = 2T (n/2) + θ(n)

• The above recurrence relation is non-homogenous and can be solved by any of the methods

– Defining characteristics polynomial

(30)

n

n

T

n

T

)

2

(

.

2

)

(

2

)

2

(

.

2

)

2

(

n

T

n

2

n

T

2 3

2

)

2

.

(

2

)

2

2

(

n

T

n

n

T

. . . 2 ) 2 ( . 2 ) 2

( n3 T n4 n3

T  

n n

n

(31)

n

n

n

T

n

n

T

n

T

)

2

(

.

2

)

(

)

2

(

.

2

)

(

2 2

n

n

n

n

T

n

T

)

2

(

.

2

)

(

3 3

n

n

n

T

n

T

)

2

(

.

2

)

(

2 2

.

.

.

(32)

• T(n) = 2T(n/2) + n

• = 2(2T(n/4) + n/2) + n = 4T(n/4) + n + n = 4T(n/4) + 2n

• = 4(2T(n/8) + n/4) + 2n = 8T(n/8) + n + 2n = 8T(n/8) + 3n

• = 8(2T(n/16) + n/8)+ 3n = 8T(n/16)+ n + 3n = 16T(n/16) + 4n

• ... = 32T(n/32) + 5n

• ...

• = n*T(1) + log2(n)*n

References

Related documents

 The total quantity of PAHs detected in the dry season are higher than those of the rainy season in marshy soil samples collected in Warri city in this study period. 

In mice subjected to PSNL, repeated once daily intrathecal injection of TAT-GESV (10 nmol i.t.) and MK-801 (5 nmol i.t.) across eight consecutive days elevated (A) mechanical

I understand that if the person or entity receiving this information is not a health plan or health care provider covered by the federal privacy regulations, the information may

In the previous study there was a significant association from 18 months to 3 years between motor skills and later language performance, but neither gross nor fine motor performance

Examples of foods which demonstrate that food is good for health are as follows:.. Jangguk-juk (porridge with

Dimitrova R, Gibson J, Turkel C, BONT-A CDH Study Group (2005) Botulinum toxin type A (BOTOX) for the prophylactic treatment of chronic daily headache: a randomized, double-

Finally, we provided a proof-of-concept by combining rep B and placI for expression of GFP gene in Shewanella , and followed by demonstrated expression of Mtr path- way as