QSORT(3POSIX)

πŸ¦‹ QSORT(3POSIX) β€” the POSIX Programmer's Manual glow-up πŸ¦‹

okay bestie, quick vibe check before we dive in: this page is part of the POSIX Programmer's Manual. Heads up tho β€” the Linux implementation of this interface may differ (so like, go read the corresponding Linux manual page for the deets on Linux behavior), or honestly the interface might just not be implemented on Linux at all. πŸ™ˆ


✨ NAME ✨

qsort β€” sort a table of data. that's it. that's the whole job. iconic behavior honestly.


🌸 SYNOPSIS 🌸

#include <stdlib.h>

void qsort(void *base, size_t nel, size_t width,
    int (*compar)(const void *, const void *));

πŸ’– DESCRIPTION πŸ’–

so first thing's first: everything described on this reference page is aligned with the ISO C standard. If there's any conflict between what's written here and the ISO C standard, that conflict is unintentional β€” and this volume of POSIX.1‐2017 defers to the ISO C standard. so if they ever beef, ISO C wins. periodt.

The qsort() function shall sort an array of nel objects, and the initial element of that array is pointed to by base. The size of each object, in bytes, is specified by the width argument. And if nel has the value zero? Then the comparison function pointed to by compar shall not be called and no rearrangement shall take place. nothing happens. we love a clean no-op. πŸ’…

The application shall ensure that the comparison function pointed to by compar does not alter the contents of the array. The implementation may reorder elements of the array between calls to the comparison function, but it shall not alter the contents of any individual element. like, it can shuffle the seats but it cannot redecorate the passengers, ykwim?

When the same objects (consisting of width bytes, irrespective of their current positions in the array) are passed more than once to the comparison function, the results shall be consistent with one another. That is, they shall define a total ordering on the array. no flip-flopping, bestie, we're not doing that here. 🚫

The contents of the array shall be sorted in ascending order according to a comparison function. The compar argument is a pointer to the comparison function, which gets called with two arguments that point to the elements being compared. The application shall ensure that the function returns an integer less than, equal to, or greater than 0, if the first argument is considered respectively less than, equal to, or greater than the second. And if two members compare as equal? Their order in the sorted array is unspecified. so don't come for me about ties, okay? 😭


🌈 RETURN VALUE 🌈

The qsort() function shall not return a value. it's a void girlie. gives nothing back. 🫢


πŸŽ€ ERRORS πŸŽ€

No errors are defined.

The following sections are informative. (yes that's the actual vibe, they said it themselves πŸ’€)


⭐ EXAMPLES ⭐

None.

yeah. they really just put "None." and moved on. main character energy of them honestly. πŸ‘‘


🧚 APPLICATION USAGE 🧚

The comparison function need not compare every byte, so arbitrary data may be contained in the elements in addition to the values being compared. so like, your struct can have extra stuff in it that compar literally never looks at. she's got a whole life outside of you, respect it. 🌸


πŸ’œ RATIONALE πŸ’œ

okay this part is lowkey the good part. The requirement that each argument (hereafter referred to as p) to the comparison function is a pointer to elements of the array implies that for every call, for each argument separately, all of the following expressions are non-zero:

((char *)p - (char *)base) % width == 0
(char *)p >= (char *)base
(char *)p < (char *)base + nel * width

like. every single one. separately. for every call. non-zero. no cap. πŸ”₯


πŸ¦„ FUTURE DIRECTIONS πŸ¦„

None.

nothing coming. the future is empty. we stay present. ✨


πŸ‘‘ SEE ALSO πŸ‘‘

  • alphasort()
  • The Base Definitions volume of POSIX.1‐2017, <stdlib.h>

Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html .

Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .


and that's the whole page, bestie πŸ’… this edition dropped 2017, brought to you by IEEE/The Open Group, and it's QSORT(3POSIX) all the way down. go sort something. slay. πŸ¦‹πŸ’–