[英]What sorting algorithm does qsort use?
I can't find any information regarding what sorting algorithm C qsort
function uses. 我找不到有关C qsort
函数使用哪种排序算法的任何信息。
Is it quicksort? 是quicksort吗? It is not mentioned in man. 没有在人中提到。
The implementation of qsort
is not specified: an implementation may use any sorting algorithm. 未指定qsort
的实现:实现可以使用任何排序算法。 Interestingly, the sort does not need to be stable, and there is no complexity requirement. 有趣的是,排序不需要是稳定的,也没有复杂性要求。
The entire specification of qsort
(C11 §7.22.5.2) is as follows: qsort
的整个规范(C11§7.22.5.2)如下:
The
qsort
functionqsort
函数Synopsis 概要
#include <stdlib.h> void qsort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *));
Description 描述
The
qsort
function sorts an array ofnmemb
objects, the initial element of which is pointed to bybase
.qsort
函数对nmemb
对象的数组进行nmemb
,该对象的初始元素由base
指向。 The size of each object is specified bysize
. 每个对象的size
由size
指定。The contents of the array are sorted into ascending order according to a comparison function pointed to by compar, which is called with two arguments that point to the objects being compared. 根据compar所指向的比较函数,数组的内容按升序排序,该比较函数由两个参数指向,该参数指向要比较的对象。 The function shall return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second. 如果第一个参数被认为分别小于,等于或大于第二个参数,则该函数应返回小于,等于或大于零的整数。
If two elements compare as equal, their order in the resulting sorted array is unspecified. 如果两个元素比较相等,则在指定的排序数组中它们的顺序是不确定的。
Returns 退货
The
qsort
function returns no value.qsort
函数不返回任何值。
In complement to James McNellis's quotation of the standard it is worth noting that GNU's libc documentation says that 作为对James McNellis对标准的引用的补充,值得注意的是GNU的libc文档指出:
The
qsort
function derives its name from the fact that it was originally implemented using the “quick sort” algorithm.qsort
函数的名称来源于它最初是使用“快速排序”算法实现的事实。
and that it decided to use an alternative algorithm , apparently a merge sort. 并且决定使用替代算法 ,显然是合并排序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.