Docsity

Θ(n lg n) Time Complexity of Merge Sort and Quick Sort Algorithms, Study notes of Data Structures and Algorithms

Partial preview of the text

Download Θ(n lg n) Time Complexity of Merge Sort and Quick Sort Algorithms and more Study notes Data Structures and Algorithms in PDF only on Docsity!

Data Structures and Algorithms^ Recursive Sorting

Chris BrooksDepartment of Computer ScienceUniversity of San Francisco

Department of Computer Science — University of San Francisco – p.1/

12-0:^ Recursive Sorting Algorithms

Basic sorting algorithms all run in

(^2) Θ(n)^ time.

We can do better by sorting sublists and combiningresults.

Department of Computer Science — University of San Francisco – p.2/

12-2:^ Example

5 1 8 2 6 4 3 7^ 5 1 2 8
5 1^
2 8^
6 4^
•^5
1 5^
2 8^
4 6^
1 2 5 8^

Department of Computer Science — University of San Francisco – p.4/

12-3:^ Merging

Merge lists into a new list,

T

Maintain three pointers (indices)

i, j,^ and

n

i^ is index of left hand list j^ is index of right hand list n^ is index of destination list

T

If^ A[i]^ < A

[j] T [n] = A[i], increment

n^ and^ i

else^ T^ [n] =

A[j], increment

n^ and^ j

Department of Computer Science — University of San Francisco – p.5/

12-5:^ Θ()

for Merge Sort

T^ (0) =^ c

1

for some constant

c^1

T^ (1) =^ c

2

for some constant

c^2

T^ (n) =^ nc

  • 2T^ (n/ 3

2)^ for some constant

c^3

T^ (n)^ =

nc+ 2^3 T^ (n/2) =^ nc+ 2(^3

n/^2 c+ 2^3

T^ (n/4)) = 2nc+ 4^3

T^ (n/4)

Department of Computer Science — University of San Francisco – p.7/

12-6:^ Θ()

for Merge Sort

T^ (0) =^ c

1

for some constant

c^1

T^ (1) =^ c

2

for some constant

c^2

T^ (n) =^ nc

  • 2T^ (n/ 3

2)^ for some constant

c^3

T^ (n)^ =

nc+ 2^3 T^ (n/2) =^ nc+ 2(^3

n/^2 c+ 2^3

T^ (n/4)) = 2nc+ 4^3

T^ (n/4) = 2nc+ 4(^3

n/^4 c+ 2^3

T^ (n/8)) = 3nc+ 8^3

T^ (n/8))

Department of Computer Science — University of San Francisco – p.8/

12-8:^ Θ()

for Merge Sort

T^ (0) =^ c

1

for some constant

c^1

T^ (1) =^ c

2

for some constant

c^2

T^ (n) =^ nc

  • 2T^ (n/ 3

2)^ for some constant

c^3

T^ (n)^ =

nc+ 2^3 T^ (n/2) =^ nc+ 2(^3

n/^2 c+ 2^3

T^ (n/4)) = 2nc+ 4^3

T^ (n/4) = 2nc+ 4(^3

n/^4 c+ 2^3

T^ (n/8)) = 3nc+ 8^3

T^ (n/8)) = 3nc+ 8(^3

n/^8 c+ 2^3

T^ (n/16)) = 4nc+ 16^3

T^ (n/16) = 5nc+ 32^3

T^ (n/32)

Department of Computer Science — University of San Francisco – p.10/

12-9:^ Θ()

for Merge Sort

T^ (0) =^ c

1

for some constant

c^1

T^ (1) =^ c

2

for some constant

c^2

T^ (n) =^ nc

  • 2T^ (n/ 3

2)^ for some constant

c^3

T^ (n)^ =

nc+ 2^3 T^ (n/2) =^ nc+ 2(^3

n/^2 c+ 2^3

T^ (n/4)) = 2nc+ 4^3

T^ (n/4) = 2nc+ 4(^3

n/^4 c+ 2^3

T^ (n/8)) = 3nc+ 8^3

T^ (n/8)) = 3nc+ 8(^3

n/^8 c+ 2^3

T^ (n/16)) = 4nc+ 16^3

T^ (n/16) = 5nc+ 32^3

T^ (n/32) =^ knc+ 2^3

kkT^ (n/^2

Department of Computer Science — University of San Francisco – p.11/

12-11:^ Θ()

for Merge Sort

T(n)

Department of Computer Science — University of San Francisco – p.13/

12-12:^ Θ()

for Merge Sort

c*n

T(n/2)

T(n/2)^ Department of Computer Science — University of San Francisco – p.14/

12-14:^ Θ()

for Merge Sort

cn c(n/2)

c*(n/2)

c*(n/4)

c*(n/4)

c*(n/4)

c*(n/4)

cn cn cn cn

...^

...^

...^

... Department of Computer Science — University of San Francisco – p.16/

12-15:^ Θ()

for Merge Sort

cn c(n/2)

c*(n/2)

c*(n/4)

c*(n/4)

c*(n/4)

c*(n/4)

cn cn cn cn

lg nleve

...^
...^
...^

... Department of Computer Science — University of San Francisco – p.17/

12-17:^ Divide & Conquer

Merge Sort:^ Divide the list two parts^ No work required – just calculate midpoint^ Recursively sort two parts^ Combine sorted lists into one list^ Some work required – need to merge lists

Department of Computer Science — University of San Francisco – p.19/

12-18:^ Divide & Conquer

Quick Sort:^ Divide the list two parts^ Some work required – Small elements in left sublist,large elements in right sublist^ Recursively sort two parts^ Combine sorted lists into one list^ No work required!

Department of Computer Science — University of San Francisco – p.20/