Is stdlib's qsort recursive?

前端 未结 9 1917
执念已碎
执念已碎 2020-12-30 09:19

I\'ve read that qsort is just a generic sort, with no promises about implementation. I don\'t know about how libraries vary from platform to plaform, but assumi

相关标签:
9条回答
  • 2020-12-30 10:19

    A qsort implementation which can fail on large arrays is extremely broken. If you're really worried I'd go RTFS, but I suspect any half-decent implementation will either use an in-place sorting algorithm or use malloc for temporary space and fall back to an in-place algorithm if malloc fails.

    0 讨论(0)
  • 2020-12-30 10:20

    I remember reading in this book: C Programming: A Modern Approach that the ANSI C specification doesn't define how to implement qsort.

    And the book wrote that qsort could in reality be a another kind of sort, merge sort, insertion sort and why not bubble sort :P

    So, the qsort implementation might not be recursive.

    0 讨论(0)
  • 2020-12-30 10:22

    You know, the recursive part is logn deep. In 64 levels of recursion (which is ~64*4=~256 bytes of stack total) you can sort an array of size ~2^64, ie an array as large as you can address on a 64 bit cpu, which is 147573952589676412928 bytes for 64 bit integers. You can't even hold it in memory!

    Worry about stuff that matters imo.

    0 讨论(0)
提交回复
热议问题