Quicksort implementation in C?

纵饮孤独 提交于 2019-12-23 15:37:26

问题


I really like the qsort function in C. It's so easy to use and allows me to procrastinate learning C++ template types. I have a few questions about this:

  • 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?
  • Are there any things I should watch out for to avoid security problems/segfaults?

回答1:


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

It is implementation dependent.

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

C does not have templates. If you need a generic sorting function in C, then qsort is a good choice.

If you are going to use C++, then you should use std::sort, which is much easier to use correctly and gives type safety.

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

If you use the function improperly (for example, if you pass it incorrect parameters or if your comparison function has bugs in it), then your program might well crash (or might otherwise perform incorrectly). Of course, this isn't specific to qsort; this is true for anything used in a program.



来源:https://stackoverflow.com/questions/3485974/quicksort-implementation-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!