Professional Documents
Culture Documents
Sorting algorithms
Douglas
Douglas Wilhelm
Wilhelm Harder,
Harder, M.Math.
M.Math. LEL
LEL
Department
Department of of Electrical
Electrical and
and Computer
Computer Engineering
Engineering
University
University of
of Waterloo
Waterloo
Waterloo,
Waterloo, Ontario,
Ontario, Canada
Canada
ece.uwaterloo.ca
ece.uwaterloo.ca
dwharder@alumni.uwaterloo.ca
dwharder@alumni.uwaterloo.ca
2006-2013
2006-2013 by
by Douglas
Douglas Wilhelm
Wilhelm Harder.
Harder. Some
Some rights
rights reserved.
reserved.
Sorting algorithms
2
Outline
In-place
8.1.1 Sorting
Classifications
8.1.2
Insertion
Exchanging
Selection
Merging
Distribution
Sorting algorithms
8
Run-time
8.1.3
The run time of the sorting algorithms we will look at fall into one of
three categories:
(n) (n ln(n)) O(n2)
Run-time
8.1.3
Lower-bound
8.1.4 Run-time
Any sorting algorithm must examine each entry in the array at least
once
Consequently, all sorting algorithms must be (n)
Lower-bound
8.1.4 Run-time
Optimal
8.1.5 Sorting Algorithms
The next seven topics will cover seven common sorting algorithms
There is no optimal sorting algorithm which can be used in all places
Under various circumstances, different sorting algorithms will deliver
optimal run-time and memory-allocation requirements
Sorting algorithms
13
Sub-optimal
8.1.6 Sorting Algorithms
Sub-optimal
8.1.6 Sorting Algorithms
Inversions
8.1.7
1 16 12 26 25 35 33 58 45 42 56 67 83 75 74 86 81 88 99 95
1 17 21 42 24 27 32 35 45 47 57 23 66 69 70 76 87 85 95 99
22 20 81 38 95 84 99 12 79 44 26 87 96 10 48 80 1 31 16 92
Inversions
8.1.7
1 16 12 26 25 35 33 58 45 42 56 67 83 75 74 86 81 88 99 95
1 12 16 25 26 33 35 42 45 56 58 67 74 75 81 83 86 88 95 99
Sorting algorithms
17
Inversions
8.1.7
1 17 21 42 24 27 32 35 45 47 57 23 66 69 70 76 87 85 95 99
1 17 21 23 24 27 32 35 42 45 47 57 66 69 70 76 85 87 95 99
Inversions
8.1.7
22 20 81 38 95 84 99 12 79 44 26 87 96 10 48 80 1 31 16 92
1 10 12 16 20 22 26 31 38 44 48 79 80 81 84 87 92 95 96 99
Sorting algorithms
19
Inversions
8.1.7
Inversions
8.1.7
Inversions
8.1.7
Inversions
8.1.7
Inversions
8.1.7
Inversions
8.1.7
1 3 5 2 4 6
removes the inversion (4, 2)
or introduces a new inversion, e.g., (5, 3) with
1 3 5 4 2 6
1 5 3 4 2 6
Sorting algorithms
25
Number
8.1.7.1 of Inversions
n n n 1
There are pairs of numbers in any set of n objects
2
2
Number
8.1.7.1 of Inversions
Number
8.1.7.1 of Inversions
20 20 20 1
There are 190 pairs
2
2
Number
8.1.7.1 of Inversions
Number
8.1.7.1 of Inversions
Number
8.1.7.1 of Inversions
Summary
References
Wikipedia, http://en.wikipedia.org/wiki/Sorting_algorithm
http://en.wikipedia.org/wiki/Sorting_algorithm#Inefficient.2Fhumorous_sorts
[1] Donald E. Knuth, The Art of Computer Programming, Volume 3: Sorting and Searching,
2nd Ed., Addison Wesley, 1998, 5.1, 2, 3.
[2] Cormen, Leiserson, and Rivest, Introduction to Algorithms, McGraw Hill, 1990, p.137-9
and 9.1.
[3] Weiss, Data Structures and Algorithm Analysis in C++, 3rd Ed., Addison Wesley, 7.1,
p.261-2.
[4] Gruber, Holzer, and Ruepp, Sorting the Slow Way: An Analysis of Perversely Awful
Randomized Sorting Algorithms, 4th International Conference on Fun with Algorithms,
Castiglioncello, Italy, 2007.
These slides are provided for the ECE 250 Algorithms and Data Structures course. The
material in it reflects Douglas W. Harders best judgment in light of the information available to
him at the time of preparation. Any reliance on these course slides by any party for any other
purpose are the responsibility of such parties. Douglas W. Harder accepts no responsibility for
damages, if any, suffered by any party as a result of decisions made or actions based on these
course slides for any other purpose than that for which it was intended.