Bifalgorithm post · Computer science one · Sorting

How does the C standard library’s qsort() function work?

How does the C standard library’s qsort() function work? (Computer science one · Sorting) — "The standard library also provides a generic sorting function, qso…

Ideas in this flowchart

  • qsort() : fonction générique de tri de la bibliothèque standard
  • qsort() trie un tableau d’éléments selon une fonction de comparaison.
  • Le nom qsort() ne garantit pas l’utilisation de Quick Sort.
  • Les implémentations modernes peuvent utiliser Merge Sort ou un Quick Sort hybride non récursif.
  • Son prototype et ses paramètres ressemblent à ceux des fonctions de recherche, mais sans paramètre key.
  • Le tableau n’est pas const, car son contenu sera modifié par le tri.
  • Les éléments sont classés par ordre croissant selon le comparateur fourni.
  • Prototype : void qsort(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *))
  • base : pointeur vers un tableau d’éléments.
  • nmemb : nombre d’éléments du tableau.
  • size : taille en octets de chaque élément, généralement obtenue avec sizeof().
  • compar : fonction de comparaison utilisée pour ordonner les éléments.
Feed/

How does the C standard library’s qsort() function work?

No cover

Channel

Computer science one

1 page

"The standard library also provides a generic sorting function, qsort(). Though the name suggests a Quick Sort implementation, it does not necessarily have to be (it was when the function was originally designed). Modern implementations of qsort() may implement alternatives such as Merge Sort or non-recursive hybrid Quick Sort algorithms. The prototype and parameters are similar to the search functions but do not include a key. The array is also not const indicating that it will be changed (which is the whole point of calling the function). The function will sort elements in ascending order according to the provided comparator function.\n\nvoid qsort(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *))\n\n• base – pointer an array of elements\n\n• nmemb – the size of the array (number of members)\n\n• size – the size (in bytes) of each element (use sizeof())\n\n• compar – a comparator function used to order elements"

Flowchart preview
👍
0
0 comments0 shares

Sign in to view and post comments.