next up previous
Next: Chapter 9: Graph Algorithms Up: CS 3345: Algorithm Analysis Previous: Chapter 6: Priority Queues

Chapter 7: Sorting

Sorting
Goal: Sort a set of n elements.



Assumption: No element is repeated.



Insertion Sort

Shellsort

Heapsort
Mergesort

Quicksort
Recursive Solution: Quicksort(S)
1.
pick any element v as pivot
2.
partition
3.
return {Quicksort(S1,), v, Quicksort(S2)}
Base Case: if $\mid S \mid = 0$, or 1, return {S}

Picking the pivot

Quicksort: Time Complexity

T(n) = T(i) + T(n-i-1) + cn


T(0) = 1

Worst Case:
pivot is always the smallest
element
T(n) = O(n2)
Best Case:
equal partitions
T(n) = 2 T(n/2) + cn
T(n) = O(n log n)
Average Case:
each size of S1 is equally likely
$T(n) = \frac{2}{n}(\sum_{j=0}^{n-1} T(j)) + cn$
T(n) = O(n log n)

Quickselect(S1, k)
Select kth smallest element from S
1.
pick pivot $v \in S$
2.
partition $S-\{v\}$ into S1 and S2
3.
if $k\leq \mid S_1 \mid$, Quickselect(S1, k)
4.
if $k = \mid S_1 \mid + 1$, return(pivot)
5.
otherwise, Quickselect(S2, $k-\mid S_1 \mid -1$)

Sorting Large Objects

External Sorting


next up previous
Next: Chapter 9: Graph Algorithms Up: CS 3345: Algorithm Analysis Previous: Chapter 6: Priority Queues
Ravi Prakash
1999-11-17