





























































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 69
This page cannot be seen from the preview
Don't miss anything!






























































Chris Brooks
Department of Computer Science
University of San Francisco
Department of Computer Science — University of San Francisco – p.1/
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/
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/
Pseudocode:
Pick a pivot index Swap A[pivotindex] and A[high] Set
i^
low
,^ j
high
while
(i <
j)
while
[i]
[pivot
], increment
i
while
[j]
[pivot
], decrement
i
swap
[i]
and
[j]
increment
i, decrement
j
swap
[i]
and
[pivot
Department of Computer Science — University of San Francisco – p.5/
Worst case performance occurs when break list into size
n
and size
c^1
for some constant
c^1
c^2
for some constant
c^2
n) =
nc
(n
for some constant
c^3
n)
nc
(n
n^ −
nc
c^2
Department of Computer Science — University of San Francisco – p.7/
Worst case:
(n
(n
nc
c^2
n) =^
n^ −
nc
c^2
Department of Computer Science — University of San Francisco – p.8/
Worst case:
(n
(n
nc
c^2
n) =^
n^ −
nc
c^2
n^ −
n^ −
c^3
c] +^2
nc
c^2
n^ −
n^ + (
n^ −
c^3
c^2
n^ −
n^ −
c^3
c] + (^2
n^ + (
n^ −
c^3
c^2
n^ −
n^ + (
n^ −
n^ −
c^3
c^2 Department of Computer Science — University of San Francisco – p.10/
Worst case:
(n
(n
nc
c^2
n) =^
n^ −
nc
c^2
n^ −
n^ −
c^3
c] +^2
nc
c^2
n^ −
n^ + (
n^ −
c^3
c^2
n^ −
n^ −
c^3
c] + (^2
n^ + (
n^ −
c^3
c^2
n^ −
n^ + (
n^ −
n^ −
c^3
c^2
n^ −
n^ + (
n^ −
n^ −
n^ −
c^3
c^2
Department of Computer Science — University of San Francisco – p.11/
Worst case: T^ (
n) =
(n
k) + (
k−
1 i=
(n
i)
c^3
kc
2
Set
k^
n:
T^ (
n)
n^ −
k) + (
k−
1 i=
(n
i)
c) +^3
kc
2
n^ −
n) + (
n−
1 i=
(n
i)
c) +^3
kc
2
n−
1 i=
(n
i)
c^3
kc
2
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/
Best case performance occurs when break list into size b(
n^ −
c^ and size
d(
n^ −
e
c^1
for some constant
c^1
c^2
for some constant
c^2
n) =
nc
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 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/
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/
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/
Building the heap takes time
n)
Each of the
n
RemoveMax calls takes time
(lg
n)
Total time:
(n
lg
n)
(also
n^ lg
n)
Department of Computer Science — University of San Francisco – p.20/