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

This document from the university of san francisco's computer science department provides an in-depth analysis of various sorting algorithms, including quick sort, merge sort, counting sort, and radix sort. It discusses their time complexities, best and worst-case scenarios, and examples. The document also explores techniques to improve quick sort's performance and compares counting sort and radix sort to the general ω(n lg n) bound for all sorting algorithms.

Typology: Study notes

Pre 2010

Uploaded on 07/30/2009

koofers-user-2er
koofers-user-2er 🇺🇸

10 documents

1 / 69

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
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/69
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
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45

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/