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

An in-depth analysis of the time complexity of merge sort and quick sort algorithms, both of which run in θ(n lg n) time. It covers the base case, recursive case, and merging process of merge sort, as well as the divide and conquer approach of both algorithms. The worst-case performance of quick sort is also discussed, along with methods to improve its performance.

Typology: Study notes

Pre 2010

Uploaded on 07/30/2009

koofers-user-wvc-1
koofers-user-wvc-1 🇺🇸

10 documents

1 / 45

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Data Structures and Algorithms
Recursive Sorting
Chris Brooks
Department of Computer Science
University of San Francisco
Department of Computer Science University of San Francisco p.1/45
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

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/