c - C 中的快速排序实现?

标签 c quicksort qsort

我真的很喜欢 C 语言中的 qsort 函数。它非常容易使用,让我可以推迟学习 C++ 模板类型。我对此有几个问题:

  • 所使用的算法是否始终是快速排序,还是依赖于编译器实现?
  • 您会推荐使用此功能吗?模板是否有真正的好处?
  • 为了避免安全问题/段错误,我应该注意哪些事项?

最佳答案

Is the algorithm used always a quicksort or is it compiler-implementation-dependent?

它取决于实现。

Would you recommend using this function or is there a real benefit to templates?

C 没有模板。如果您需要 C 语言中的通用排序函数,那么 qsort 是一个不错的选择。

如果您要使用 C++,那么您应该使用 std::sort,它更容易正确使用并提供类型安全性。

Are there any things I should watch out for to avoid security problems/segfaults?

如果您不正确地使用该函数(例如,如果您向其传递了不正确的参数或者您的比较函数中存在错误),那么您的程序很可能会崩溃(或者可能会执行不正确)。当然,这并不是 qsort 特有的;对于程序中使用的任何东西都是如此。

关于c - C 中的快速排序实现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3485974/

相关文章:

c - Turbo C 3.0 遵循哪个版本的 ANSI C 标准?

qsort 比较函数中的 const 限定符

C语言: why when input a float number in an int declared variable the result varries

c - 分配失败怎么办?

java - Swift 快速排序算法

c++ - 编译时快速排序 : Passing comparer as template parameter

performance - 快速排序与。归并排序性能分析

c - 优化排序

c++ - Qsort比较函数

c++ - 为什么gcc会有一个long long的警告?