Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why quicksort is more popular than radix-sort?

Why quicksort(or introsort), or any comparison-based sorting algorithm is more common than radix-sort? Especially for sorting numbers.

Radix-sort is not comparison based, hence may be faster than O(nlogn). In fact, it is O(kn), where k is the number of bits used to represent each item. And the memory overhead is not critical, since you may choose the number of buckets to use, and required memory may be less than mergesort's requirements.

Does it have to do with caching? Or maybe accessing random bytes of integers in the array?

like image 809
Daniyar Avatar asked Aug 21 '10 22:08

Daniyar


People also ask

Why is quick sort used more than radix sort?

Quicksort and Introsort work well with all kinds of data. All you need for sorting is the possibility to compare items. This is trivial with numbers but you can sort other data as well. Radix sort on the other hand just sorts things by their binary representation.

Is quicksort faster than radix sort?

The benchmark shows the MSB in-place radix sort to be consistently over 3 times faster than quicksort for large arrays. It's also significantly faster for smaller arrays, but with more variable results probably due to various caching effects.

Why is quicksort so popular?

Typically, quicksort is significantly faster in practice than other O(nlogn) algorithms, because its inner loop can be efficiently implemented on most architectures, and in most real-world data, it is possible to make design choices that minimize the probability of requiring quadratic time.


2 Answers

One obvious answer is that you can sort arbitrary types using quicksort (ie anything that's comparable), while you are restricted to numbers only with radix. And IMO quicksort is a lot more intuitive.

like image 26
NullUserException Avatar answered Sep 24 '22 08:09

NullUserException


Two arguments come to my mind:

  1. Quicksort/Introsort is more flexible:

    Quicksort and Introsort work well with all kinds of data. All you need for sorting is the possibility to compare items. This is trivial with numbers but you can sort other data as well.

    Radix sort on the other hand just sorts things by their binary representation. It never compares items against each other.

  2. Radix sort needs more memory.

    All radix sort implementations that I've seen use a secondary buffer to store partial sorting results. This increases the memory requirements of the sorting algorithm. That may not be a problem if you only sort a couple of kilobytes, but if you go into the gigabyte range it makes a huge difference.

    If I remember right a in place radix-sort algorithm exist on paper though.

like image 103
Nils Pipenbrinck Avatar answered Sep 22 '22 08:09

Nils Pipenbrinck