On 2022-10-26 07:54, Jonathan Leffler wrote:
There's no need to sort arrays of size 0 or 1.
Good point, thanks. I installed the first attached patch to do that. The second patch just fixes spacing as a style matter. Although there's a similar issue with bsearch and arrays with zero members, I didn't add a size-zero check there, as the base pointer must be nonnull when bsearch is called, the C standard says that bsearch does the right thing if you give it a valid non-null pointer and a zero nmemb, and I didn't see much point to adding an optimization there. Whereas with qsort, the base pointer might be null so the code must check for either a null pointer or size zero anyway, so why not check for size 1 as that's nearly as cheap and arguably clearer?