Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/algorithm/qsort"

From cppreference.com
< cpp‎ | algorithm
(Added LWG issue #405 DR.)
m (Minor tweak.)
 
(3 intermediate revisions by 3 users not shown)
Line 4: Line 4:
 
{{dcl header|cstdlib}}
 
{{dcl header|cstdlib}}
 
{{dcl|num=1|
 
{{dcl|num=1|
void qsort( void *ptr, std::size_t count, std::size_t size, /* compare-pred */* comp );
+
void qsort( void *ptr, std::size_t count,
void qsort( void *ptr, std::size_t count, std::size_t size, /* c-compare-pred */* comp );
+
            std::size_t size, /* c-compare-pred */* comp );
 +
void qsort( void *ptr, std::size_t count,
 +
            std::size_t size, /* compare-pred */* comp );
 
}}
 
}}
{{dcl|num=2|1=
+
{{dcl|num=2|notes={{mark expos}}|1=
extern "C++" using /* compare-pred */ = int(const void*, const void*); // exposition-only
+
extern "C" using /* c-compare-pred */ = int(const void*, const void*);
extern "C" using /* c-compare-pred */ = int(const void*, const void*); // exposition-only
+
extern "C++" using /* compare-pred */ = int(const void*, const void*);
 
}}
 
}}
 
{{dcl end}}
 
{{dcl end}}
Line 31: Line 33:
  
 
===Notes===
 
===Notes===
Despite the name, C++, C, and POSIX standards do not require this function to be implemented using [[enwiki:Quicksort|quicksort]] or make any complexity or stability guarantees.
+
Despite the name, C++, C, and POSIX standards do not require this function to be implemented using {{enwiki|Quicksort}} or make any complexity or stability guarantees.
  
 
The two overloads provided by the C++ standard library are distinct because the types of the parameter {{c|comp}} are distinct ({{lt|cpp/language/language linkage}} is part of its type).
 
The two overloads provided by the C++ standard library are distinct because the types of the parameter {{c|comp}} are distinct ({{lt|cpp/language/language linkage}} is part of its type).
Line 39: Line 41:
 
|The following code sorts an array of integers using {{tt|qsort()}}:
 
|The following code sorts an array of integers using {{tt|qsort()}}:
 
|code=
 
|code=
#include <iostream>
+
#include <array>
#include <cstdlib>
+
 
#include <climits>
 
#include <climits>
 
#include <compare>
 
#include <compare>
#include <array>
+
#include <cstdlib>
 +
#include <iostream>
  
 
int main()
 
int main()
Line 69: Line 71:
 
     for (int ai : a)
 
     for (int ai : a)
 
         std::cout << ai << ' ';
 
         std::cout << ai << ' ';
 +
    std::cout << '\n';
 
}
 
}
 
|output=
 
|output=

Latest revision as of 00:13, 13 September 2023

 
 
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy, ranges::sort, ...
Execution policies (C++17)
Non-modifying sequence operations
Batch operations
(C++17)
Search operations
(C++11)                (C++11)(C++11)

Modifying sequence operations
Copy operations
(C++11)
(C++11)
Swap operations
Transformation operations
Generation operations
Removing operations
Order-changing operations
(until C++17)(C++11)
(C++20)(C++20)
Sampling operations
(C++17)

Sorting and related operations
Partitioning operations
Sorting operations
Binary search operations
(on partitioned ranges)
Set operations (on sorted ranges)
Merge operations (on sorted ranges)
Heap operations
Minimum/maximum operations
(C++11)
(C++17)
Lexicographical comparison operations
Permutation operations
C library
qsort
Numeric operations
Operations on uninitialized memory
 
Defined in header <cstdlib>
void qsort( void *ptr, std::size_t count,

            std::size_t size, /* c-compare-pred */* comp );
void qsort( void *ptr, std::size_t count,

            std::size_t size, /* compare-pred */* comp );
(1)
extern "C" using /* c-compare-pred */ = int(const void*, const void*);
extern "C++" using /* compare-pred */ = int(const void*, const void*);
(2) (exposition only*)

Sorts the given array pointed to by ptr in ascending order. The array contains count elements of size bytes. Function pointed to by comp is used for object comparison.

If comp indicates two elements as equivalent, their order is unspecified.

If the type of the elements of the array is not a PODType(until C++11)TriviallyCopyable type(since C++11), the behavior is undefined.

Contents

[edit] Parameters

ptr - pointer to the array to sort
count - number of elements in the array
size - size of each element in the array in bytes
comp - comparison function which returns ​a negative integer value if the first argument is less than the second, a positive integer value if the first argument is greater than the second and zero if the arguments are equivalent.

The signature of the comparison function should be equivalent to the following:

 int cmp(const void *a, const void *b);

The function must not modify the objects passed to it and must return consistent results when called for the same objects, regardless of their positions in the array.

[edit] Return value

(none)

[edit] Notes

Despite the name, C++, C, and POSIX standards do not require this function to be implemented using Quicksort or make any complexity or stability guarantees.

The two overloads provided by the C++ standard library are distinct because the types of the parameter comp are distinct (language linkage is part of its type).

[edit] Example

The following code sorts an array of integers using qsort():

#include <array>
#include <climits>
#include <compare>
#include <cstdlib>
#include <iostream>
 
int main()
{
    std::array a{-2, 99, 0, -743, INT_MAX, 2, INT_MIN, 4};
 
    std::qsort
    (
        a.data(),
        a.size(),
        sizeof(decltype(a)::value_type),
        [](const void* x, const void* y)
        {
            const int arg1 = *static_cast<const int*>(x);
            const int arg2 = *static_cast<const int*>(y);
            const auto cmp = arg1 <=> arg2;
            if (cmp < 0)
                return -1;
            if (cmp > 0)
                return 1;
            return 0;
        }
    );
 
    for (int ai : a)
        std::cout << ai << ' ';
    std::cout << '\n';
}

Output:

-2147483648 -743 -2 0 2 4 99 2147483647

[edit] Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior
LWG 405 C++98 the elements of the array could have any type limited to PODType

[edit] See also

searches an array for an element of unspecified type
(function) [edit]
sorts a range into ascending order
(function template) [edit]
checks if a type is trivial
(class template) [edit]
C documentation for qsort