Comparison of several sorting algorithms: Strings

(Main test page)

This test was performed with an array containing C++ strings. Each string had 50 characters, all of which had the same value except for the last 8 characters which were randomly generated (in a similar way as the integers in the first test).

Since the gcc std::string uses a copy-on-write mechanism, copying the strings is fast. This means that in this test comparing is slow and copying is fast.

The array of 1 million items was skipped because it was extremely slow to test.

100 items

100 strings

Insertion sort makes large amounts of comparisons, which penalizes it in this test, even with just 100 items. The other algorithms don't show big surprises.

Random Almost sortd Almost rev. Random end
Alg.Comp.Assig. Comp.Assig. Comp.Assig. Comp.Assig.
Insert. 25672670 418517 47044827 25672670
Shell 7671076 463747 7311092 7671076
Heap 10271743 10691882 9571558 10271743
Merge 558800 470800 424800 558800
Quick 1061475 2812241 2055335 1061475
Quick2 783724 691392 599487 783724

5000 items

5000 strings

Shell sort seems to beat heap sort in this case. The vanilla quicksort shows pathological behaviour already (the "random end" test was already so much slower than everything else that it was skipped here).

Random Almost sortd Almost rev. Random end
Alg.Comp.Assig. Comp.Assig. Comp.Assig. Comp.Assig.
Shell 101337136523 76288109844 81538118858 67958101359
Heap 107693171329 109581177595 103763161284 111988181249
Merge 5682470000 5172670000 5144470000 3461170000
Quick 9519942908 33352022003 21612331160 103132027129
Quick2 7418056380 8140232047 8109438124 13038533534

100000 items

5000 strings

At least the vanilla quicksort behaves consistently, unlike the optimized one which, once again quite unexplicably, cannot handle the last test properly. Curiously even std::sort shows signs of difficulty with the last case.

Random Almost sortd Almost rev. Random end
Alg.Comp.Assig. Comp.Assig. Comp.Assig. Comp.Assig.
Shell 38807064877275 28910933851415 29229843950486 16433182599076
Heap 30195414724686 30146094775544 29377964518734 31323414973705
Merge 15665071800000 14646641800000 14639731800000 8793071800000
Quick 25493901153934 9788949582005 6086723840932 --
Quick2 20161391433387 2157357776719 2214806908138 33061360554214

Conclusion

Shell sort shows its power over heap sort (albeit slightly). It might be safe to conclude that shell sort may be even a better choice than heap sort when comparison is slow.

Slow comparison seems to accentuate the difficult cases for quicksort. Even std::sort started showing signs of problems handling the difficult "random end" case.

Due to the low amount of comparisons performed by merge sort and its consistent amount of assignments, it performs very well in this case, where comparison is slow and assignment fast.


Previous - Next