简体   繁体   English

qsort使用什么排序算法?

[英]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 function qsort函数

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 of nmemb objects, the initial element of which is pointed to by base . qsort函数对nmemb对象的数组进行nmemb ,该对象的初始元素由base指向。 The size of each object is specified by size . 每个对象的sizesize指定。

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 theory, qsort is only defined to the point of the return values and call values of qsort and bsort . 从理论上讲,qsort仅定义为qsortbsort的返回值和调用值的bsort Here are ISO standard references. 是ISO标准参考。

In practice, it usually uses quicksort . 实际上,它通常使用quicksort

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM