next up previous contents
Next: Bin, Bucket, Radix Sorts Up: Sorting Previous: Treesort   Contents

Summary of comparison-based sorting algorithms

Here is a summary of the algorithms we have considered:

  strategy objects worst case average case
algorithm employed manipulated complexity complexity
Selection Sort Selection arrays $ O(n^2)$ $ O(n^2)$
Insertion Sort Insertion arrays/lists $ O(n^2)$ $ O(n^2)$
Bubblesort Exchange arrays $ O(n^2)$ $ O(n^2)$
Heapsort Selection arrays $ O(n \log n)$ $ O(n \log n)$
Quicksort D & C arrays $ O(n^2)$ $ O(n \log n)$
Mergesort D & C lists/arrays $ O(n \log n)$ $ O(n \log n)$
Treesort Insertion lists $ O(n^2)$ $ O(n \log n)$

But what does it all mean in practice? Here is a table comparing the performance of those of the above algorithms that operate on arrays. It should again be pointed out that these numbers are only of restricted significance, since they can vary somewhat from machine (and language implementation) to machine. Quicksort2 and mergesort2 are algorithms where the recursive procedure is abandoned in favour of Selection Sort once the size of the array falls to 16 and below.

algorithm/size 128 256 512 1024 O1024 R1024 2048
Selection Sort 12 45 164 634 643 833 2497
Insertion Sort 15 69 276 1137 6 2200 4536
Bubblesort 54 221 881 3621 1285 5627 14497
Heapsort 21 45 103 236 215 249 527
Quicksort 12 27 55 112 1131 1200 230
Quicksort2 6 12 24 57 1115 1191 134
Mergesort 18 36 88 188 166 170 409
Mergesort2 6 22 48 112 94 93 254

What has to be stressed is that there is no `best sorting algorithm'. There usually is a best sorting algorithm for particular circumstances and it is up to the program designer to make sure the appropriate one is picked.


next up previous contents
Next: Bin, Bucket, Radix Sorts Up: Sorting Previous: Treesort   Contents
Martin Escardo 2005-01-11