Generic quicksort library that can be used to sort an array having any data type (int, char *, struct key_value *, etc.).

Amit amitchoudhary0523@gmail.com
Fri Sep 26 16:29:50 GMT 2025


On Fri, Sep 26, 2025, 7:07 PM Amit <amitchoudhary0523@gmail.com> wrote:

> On Fri, 26 Sept 2025 at 18:19, Samuel Thibault <
> samuel.thibault@aquilenet.fr> wrote:
>
>> Amit, le ven. 26 sept. 2025 18:11:52 +0530, a ecrit:
>> > Generic quicksort library that can be used to sort an array having any
>> data
>> > type (int, char *, struct key_value *, etc.)
>>
>> You mean https://www.man7.org/linux/man-pages/man3/qsort.3.html
>> ?
>>
>
> It is similar (swap function is missing, so the qsort() function is itself
> swapping bytes).
>
> But what about generic linked list, generic hash, generic binary search
> tree, generic binary balanced tree, generic B+ trees, generic dynamic
> array, bits library, etc. These all also should have been there.
>
> This is interesting: ("generic_doubly_linked_list_library.c" can be used
> as a C++ STL list, map, set, stack, queue, deque, multimap, multiset,
> unordered_map, unordered_set, unordered_multimap, and unordered_multiset
> (basically as all C++ STL data structures except vector)):
> https://marc.info/?l=linux-kernel&m=174980160830856&w=2
>
> Not related to the original topic but interesting article:
> https://blog.qualys.com/vulnerabilities-threat-research/2024/01/30/qualys-tru-discovers-important-vulnerabilities-in-gnu-c-librarys-syslog
>
> But I am not blaming anyone.
>
> My idea is that all these can be done now also.
>
> C++ has many of these and that's why C++ has overtaken C.
>
> I don't like C++. I like C and that's why I sent the mail.
>
> I can only suggest. If I were the controlling authority of the C standard,
> then I would have done these even now (in 2025).
>
> ----
>


I "quickly" saw the code of qsort() here:
https://elixir.bootlin.com/glibc/glibc-2.42.9000/source/stdlib/qsort.c#L197

Issues:

1. It looks like it is not doing quicksort, it is actually doing mergesort.

That's why I was wondering how it can do both descending and ascending
order sorting in one function depending on what the comparator function is
returning.

In quicksort, you need two different functions - one for ascending order
and another one for descending order.

Mergesort can do both in one function depending on what the comparator
function is returning.

So, there is no quicksort here.

2. Swapping code is complex. It would have been much easier to take the
swap function from the user.

3. It is allocating memory. I didn't see why it is allocating memory but it
doesn't look good. It can run out of memory.

This is misleading. It is mergesort. It should have been called msort().

Then there is one more issue - if memory allocation fails then it does
"heapsort".

But no quicksort out there.

----
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://sourceware.org/pipermail/libc-alpha/attachments/20250926/26450a02/attachment.htm>


More information about the Libc-alpha mailing list