11.10 Experimental Results 267
11.10 Experimental Results
In Volume 3 of The Art of Computer Programming, Knuth presents empirical data
on the performance of various sorting algorithms as he had implemented them. We
have done our own simple implementations of bubblesort, insertionsort, heapsort,
and mergesort in Java, and have counted the number of comparisons and the num-
ber of swaps when run on data arrays of lengths 127, 511, 2,047, 8,191, 32,767, and
113,071. We generated “reasonably random”
1
arrays of these lengths of integers less
than 99,991 (the largest prime smaller than 100,000).
The raw data from these tests are presented in Figure 11.5. We show the number
of comparisons made and the number of swaps for each of the four sorting methods
on our six (ostensibly) random data sets.
Bubblesort Insertionsort
Data Comps Swaps Comps Swaps
127 8,001 4,024 4,146 4,024
511 130,305 64,184 64,690 64,184
2,047 2,094,081 1,058,276 1,060,314 1,058,276
8,191 33,542,145 16,804,307 16,812,488 16,804,307
32,767 536,821,761 268,317,072 268,349,824 268,317,072
131,071 8,589,737,985 4,054,936,240 4,296,967,076 4,296,836,017
Heapsort Quicksort
Data Comps Swaps Comps Swaps
127 1,423 836 930 215
511 7,826 4,416 4,883 1,100
2,047 39,431 21,707 24,596 5,430
8,191 190,910 103,572 121,762 25,365
32,767 894,810 479,714 594,064 116,024
131,071 4,103,982 2,181,680 2,665,459 523,552
FIGURE 11.5 Raw counts of comparisons and swaps.
We have theorems that claim that the worst-case running times for these four
sorts are O(N
2
)andO(N lg N). If our “random” data produced running times like
1
Our generation method would almost certainly not hold up under a rigorous test of random-
nicity, but we believe it is sufficient for this illustrative purpose.