Sorting Programs -

Fruit flies of algorithm analysis


Min (or max) selection sort

Simple insertion sort

Bubble sort

Quicksort

Heapsort

Mergesort

Bucket sort

Radix sort


Use selection sort for very fast programming, but only for a relatively small number of elements.

O(N2)

In-place

(Insertion or bubble sort are equivalent.)


Other Sorting Options...

Note:

We discuss sorting as if we were just sorting numbers.

Often the numbers are just a part (called the "key") of a more complex object or "record".


Some Ways to Speed Up

- Divide & Conquer

(cleave rather than whittle)

- Data Structuring

(tree rather than list)

- "Digital" Representations

exploit linear addressing princ.


Bucket Sort

Use when applicable:

Small range of integer values (or equivalent).

O(N) time to sort N items

Extra space is required ("buckets")


Radix Sort

An alternated to bucket sort for larger (but not too large) range of integers.

Effectively a multi-pass bucket sort

O(N) time

Extra space is required


Quicksort

Prototypical divide & conquer

In-place (no extra space)

O(N logN) "average" time

O(N2) worst case time


Quicksort Operation

O(N logN) "best" case

O(N2) worst case


HeapSort

O(N logN) without qualification

In-Place

Use tree to great advantage

Non-recursive

Based on "Peter Principle"


2 meanings of "heap" in CS:


Heap for Storage Allocation (Digression)

Typical memory layout


Typical Heap State

Calls to new find free space large enough.

C++: delete releases space

Java: space released when not accessible