Radix Sort Sorting in The C++ STL: CS 311 Data Structures and Algorithms Lecture Slides
Radix Sort Sorting in The C++ STL: CS 311 Data Structures and Algorithms Lecture Slides
Radix Sort Sorting in The C++ STL: CS 311 Data Structures and Algorithms Lecture Slides
Glenn G. Chappell
Department of Computer Science
University of Alaska Fairbanks
[email protected]
Major Topics
Introduction to Analysis of Algorithms
Introduction to Sorting
Comparison Sorts I
More on Big-O
The Limits of Sorting
Divide-and-Conquer
Comparison Sorts II
Comparison Sorts III
Radix Sort
Sorting in the C++ STL
Efficiency
General: using few resources (time, space, bandwidth, etc.).
Specific: fast (time).
Analyzing Efficiency
Determine how the size of the input affects running time, measured in
steps, in the worst case.
Scalable
Works well with large problems.
Efficiency
Quicksort is O(n2).
Quicksort has a very good O(n log n) average-case time.
Requirements on Data
Non-trivial pivot-selection algorithms (median-of-three and others)
are only efficient for random-access data.
Space Usage
Quicksort uses space for recursion.
Additional space: O(log n), if you are clever about it.
Even if all recursion is eliminated, O(log n) additional space is used.
This additional space need not hold any data items.
Stability
Efficient versions of Quicksort are not stable.
Performance on Nearly Sorted Data
A non-optimized Quicksort is slow on nearly sorted data: O(n2).
Median-of-three Quicksort is O(n log n) on most nearly sorted data.
Efficiency
Introsort is O(n log n).
Introsort also has an average-case time of O(n log n) [of course].
Its average-case time is just as good as Quicksort.
Requirements on Data
Introsort requires random-access data.
Space Usage
Introsort uses space for recursion (or simulated recursion).
Additional space: O(log n) even if all recursion is eliminated.
This additional space need not hold any data items.
Stability
Introsort is not stable.
Performance on Nearly Sorted Data
Introsort is not significantly faster or slower on nearly sorted data.
TO DO
Write a function to do Pigeonhole Sort.
TO DO
Write Radix Sort for small-ish positive integers.
Comments
Radix Sort makes very strong assumptions about the values
in the list to be sorted.
It requires linear additional space.
It is stable.
It does not perform especially well or badly on nearly sorted
data.
Of course, what we really care about is speed. See the next
slide.
100 million
records to sort by
ZIP code? Radix
Sort works well.
template<typename Iterator>
void sortIt(Iterator first, Iterator last);
We will look at the last two STL algorithms in more detail later in
the semester, when we cover Priority Queues and Heaps:
Functions std::partial_sort and std::partial_sort_copy, in
<algorithm>
Global functions.
Take three random-access iterators and an optional comparison.
O(n log n). Not stable.
Solve a more general problem than comparison sorting.
Algorithm used: probably Heap Sort.
Combination: std::make_heap & std::sort_heap, in <algorithm>
Both Global functions.
Both take two random-access iterators and an optional comparison.
Combination is O(n log n). Not stable.
Solves a more general problem than comparison sorting.
Algorithm used: almost certainly Heap Sort.
vector<int> v;
Default constructor call.
std::sort(v.begin(), v.end()); We can only pass an
// Ascending order object, not a type.
#include <list>
std::list<int> myList;
myList.sort(); // Ascending order
myList.sort(std::greater<int>()); // Descending order