Domanda

Does the function qsort() in stdlib.h actually use quicksort algorithm, as the name suggests?

È stato utile?

Soluzione

The qsort() function may be implemented using any sort algorithm that the library implementer chooses, but the name suggests that the algorithm should be close to optimal. Using an O(N2) algorithm would be permissible, but a major QoI (quality of implementation) issue.

It is worth noting that comparison is quite expensive with the qsort() interface; any sort algorithm that increases the number of comparisons to reduce the number of moves (which can also be expensive if you are not shuffling pointers) may lead to worse performance. However, that's an issue for the library implementer to be concerned with. Unless you find that the library implementation is horrid (which is pretty unlikely these days), use it and don't worry.

The C++ sort algorithm can run rings around C's qsort.

Altri suggerimenti

The C11 standard does not specify. So any reasonable O(n log n) would be acceptable.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top