Docsity

Analysis & Comparison of Sorting Algorithms: Quick, Merge, Counting, & Radix Sort, Study notes of Data Structures and Algorithms

Partial preview of the text

Download Analysis & Comparison of Sorting Algorithms: Quick, Merge, Counting, & Radix Sort and more Study notes Data Structures and Algorithms in PDF only on Docsity!

Data Structures and Algorithms

More Sorting

Chris Brooks

Department of Computer Science

University of San Francisco

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

Merge Sort

Base Case:

A list of length 1 or length 0 is already sorted Recursive Case:

Split the list in half Recursively sort two halves Merge sorted halves together

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

Quick Sort with constant

memory

Can we avoid making a duplicate of the list we’re sorting?

Swap pivot element out of the way (we’ll swap it back later) Maintain two pointers,

i^

and

j

i^ points to the beginning of the list j^ points to the end of the list Move

i^

and

j^

in to the middle of the list – ensuring that all

elements to the left of

i^

are

the pivot, and all elememnts

to the right of

j^

are greater than the pivot

Swap pivot element back to middle of list

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

Quick Sort - Partitioning

Pseudocode:

Pick a pivot index Swap A[pivotindex] and A[high] Set

i^

low

,^ j

high

−^1

while

(i <

=^

j)

while

A

[i]

< A

[pivot

], increment

i

while

A

[j]

> A

[pivot

], decrement

i

swap

A

[i]

and

A

[j]

increment

i, decrement

j

swap

A

[i]

and

A

[pivot

]

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

for Quick Sort

Worst case performance occurs when break list into size

n

and size

T^ (0) =

c^1

for some constant

c^1

T^ (1) =

c^2

for some constant

c^2

T^ (

n) =

nc

T^

(n

T^

for some constant

c^3

T^ (

n)

=^

nc

T^

(n

T^

=^

T^ (

n^ −

nc

c^2

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

for Quick Sort

Worst case:

T

(n

T

(n

nc

c^2

T^ (

n) =^

T^ (

n^ −

nc

c^2

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

for Quick Sort

Worst case:

T

(n

T

(n

nc

c^2

T^ (

n) =^

T^ (

n^ −

nc

c^2

= [

T^ (

n^ −

n^ −

c^3

+^

c] +^2

nc

c^2

=^

T^ (

n^ −

n^ + (

n^ −

c^3

c^2

= [

T^ (

n^ −

n^ −

c^3

+^

c] + (^2

n^ + (

n^ −

c^3

c^2

=^

T^ (

n^ −

n^ + (

n^ −

n^ −

c^3

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

for Quick Sort

Worst case:

T

(n

T

(n

nc

c^2

T^ (

n) =^

T^ (

n^ −

nc

c^2

= [

T^ (

n^ −

n^ −

c^3

+^

c] +^2

nc

c^2

=^

T^ (

n^ −

n^ + (

n^ −

c^3

c^2

= [

T^ (

n^ −

n^ −

c^3

+^

c] + (^2

n^ + (

n^ −

c^3

c^2

=^

T^ (

n^ −

n^ + (

n^ −

n^ −

c^3

c^2

=^

T^ (

n^ −

n^ + (

n^ −

n^ −

n^ −

c^3

c^2

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

for Quick Sort

Worst case: T^ (

n) =

T

(n

k) + (

k−

1 i=

(n

i)

c^3

kc

2

Set

k^

=^

n:

T^ (

n)

=^

T^ (

n^ −

k) + (

k−

1 i=

(n

i)

c) +^3

kc

2

=^

T^ (

n^ −

n) + (

n−

1 i=

(n

i)

c) +^3

kc

2

=^

T^ (0) + (

n−

1 i=

(n

i)

c^3

kc

2

=^

T^ (0) + (

n−

1 i=

ic

kc

2

=^

c^1

+^

c^3

n(

n^ + 1)

kc

2

∈^

(^2) n

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

for Quick Sort

Best case performance occurs when break list into size b(

n^ −

/^2

c^ and size

d(

n^ −

/^2

e

T^ (0) =

c^1

for some constant

c^1

T^ (1) =

c^2

for some constant

c^2

T^ (

n) =

nc

T^ (

n/

for some constant

c^3

This is the same as Merge Sort:

n^ lg

n)

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

Quick Sort - Worst Case

Quick Sort has worst-case performance when:

The list is sorted (or almost sorted) The list is inverse sorted (or almost inverse sorted) Many lists we want to sort are almost sorted! How can we fix Quick Sort?

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

Better Partitions

Pick the middle element as the pivot

Sorted and reverse sorted lists give good performance Pick a random element as the pivot

No single list always gives bad performance Pick the median of 3 elements

First, Middle, Last 3 Random Elements

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

Heap Sort

Build a heap out of the data Repeat:

Remove the largest element from the list, place it atend of heap Until all elements have been removed from the heap The list is now sorted Example: 3 1 7 2 5 4

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

for Heap Sort

Building the heap takes time

n)

Each of the

n

RemoveMax calls takes time

O

(lg

n)

Total time:

(n

lg

n)

(also

n^ lg

n)

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