COMP2521 20T2 ♢ Sorting (ii) [0/11]
❖ Summary of Sorting Methods | |
Sorting = arrange a collection of N items in ascending order ...
Elementary sorting algorithms: O(N2) comparisons
- selection sort, insertion sort, bubble sort
Advanced sorting algorithms:
O(NlogN) comparisons
- quicksort, merge sort, heap sort (priority queue)
Most are intended for use in-memory
(random access data structure).
Merge sort adapts well for use as disk-based sort.
COMP2521 20T2 ♢ Sorting (ii) [1/11]
❖ ... Summary of Sorting Methods | |
Other properties of sort algorithms: stable, adaptive
Selection sort:
- stability depends on implementation
- not adaptive
Bubble sort:
- is stable if items don't move past same-key items
- adaptive if it terminates when no swaps
Insertion sort:
- stability depends on implementation of insertion
- adaptive if it stops scan when position is found
COMP2521 20T2 ♢ Sorting (ii) [2/11]
❖ ... Summary of Sorting Methods | |
Other properties of sort algorithms: stable, adaptive
Quicksort:
- easy to make stable on lists; difficult on arrays
- can be adaptive depending on implementation
Merge sort:
- is stable if merge operation is stable
- can be made adaptive (but version in slides is not)
Heap sort:
- is not stable because of top-to-bottom nature of heap ordering
- adaptive variants of heap sort exist (faster if data almost sorted)
COMP2521 20T2 ♢ Sorting (ii) [3/11]
❖ Lower Bound for Comparison-Based Sorting | |
All of the above sorting algorithms for arrays of n elements
- have comparing whole keys as a critical operation
Such algorithms cannot work with less than
O(n log n) comparisons
Informal proof (for arrays with no duplicates):
- there are n! possible permutation sequences
- one of these possible sequences is a sorted sequence
- each comparision reduces # possible sequences to be considered
(continued ...)
COMP2521 20T2 ♢ Sorting (ii) [4/11]
❖ ... Lower Bound for Comparison-Based Sorting | |
Can view sorting as navigating a decision tree ...
(continued ...)
COMP2521 20T2 ♢ Sorting (ii) [5/11]
❖ ... Lower Bound for Comparison-Based Sorting | |
Can view the sorting process as
- following a path from the root to a leaf in the decision tree
- requiring one comparison at each level
For n elements, there are n! leaves
- height of such a tree is at least log2(n!)
⇒ number of comparisions required is at least log2(n!)
So, for comparison-based sorting, lower bound is Ω(n log2 n).
Are there faster algorithms not based on whole key comparison?
COMP2521 20T2 ♢ Sorting (ii) [6/11]
Radix sort is a non-comparative sorting algorithm.
Requires us to consider a key as a tuple
(k1, k2, ..., km), e.g.
- represent key 372 as (3, 7, 2)
- represent key
"sydney"
as (s, y, d, n, e, y)
Assume only small number of possible values for k
i, e.g.
- numeric: 0-9 ... alpha: a-z
If keys have different lengths, pad with suitable character, e.g.
- numeric: 123, 002, 015 ... alpha:
"abc"
, "zz⎵"
, "t⎵⎵"
COMP2521 20T2 ♢ Sorting (ii) [7/11]
Radix sort algorithm:
- stable sort on km,
- then stable sort on k(m-1),
- continue until we reach k1
Example:
COMP2521 20T2 ♢ Sorting (ii) [8/11]
Stable sorting (bucket sort):
for each i in m .. 1 do
empty all buckets
for each key in A do
append key to bucket[key[i]]
end for
clear A
for each bucket in order do
for each key in bucket do
append to array
end for
end for
end for
COMP2521 20T2 ♢ Sorting (ii) [9/11]
Example:
-
m
= 3, alphabet = {'a', 'b', 'c'}, B[ ] = buckets
- A[ ] = {"abc", "cab", "baa", "a⎵⎵", "ca⎵"}
After first pass (i
= 3):
- B['a'] = {"baa"}, B['b'] = {"cab"}, B['c'] = {"abc"}, B['⎵'] = {"a⎵⎵","ca⎵"}
- A[ ] = {"baa", "cab", "abc", "a⎵⎵", "ca⎵"}
After second pass (i
= 2):
- B['a'] = {"baa","cab","ca⎵"}, B['b'] = {"abc"}, B['c'] = {}, B["⎵"] = {"a⎵⎵"}
- A[ ] = {"baa", "cab", "ca⎵", "abc", "a⎵⎵"}
After third pass (i
= 1):
- B['a'] = {"abc","a⎵⎵"}, B['b'] = {"baa"}, B['c'] = {"cab","ca⎵"}, B["⎵"] = {}
- A[ ] = {"abc", "a⎵⎵", baa", "cab", "ca⎵"}
COMP2521 20T2 ♢ Sorting (ii) [10/11]
Complexity analysis:
- array contains n keys, each key contains m symbols
- stable sort (bucket sort) runs in time O(n)
- radix sort uses stable sort m times
So, time complexity for radix sort = O(mn)
Radix sort performs better than comparison-based sorting algorithms
- when keys are short (small m ) and arrays are large (large n )
COMP2521 20T2 ♢ Sorting (ii) [11/11]
Produced: 23 Jul 2020