Here, we will see the difference between qsort() in C language and sort() in C language.
C language provides the qsort() function, which can be used to sort arrays. The parameters and syntax of the function are shown below.
void qsort(void *base, size_t num, size_t size, int (*comparator) (const void*, const void*));
This function accepts the base address of the array, the number of elements in the array, the size of each item in the array, and a comparison function.
C provides the sort() function, which is located in the C STL. Its parameters and syntax are shown below.
void sort(T first, T last, Compare c);
There is no guarantee that the order of repeated elements is preserved. To achieve this, we can use stable_sort provided by C STL.
qsort() in C | sort() in C |
---|---|
#It uses quick sort algorithm. | It uses introsort. This is a hybrid sorting algorithm. Different implementations use different algorithms. GNU C STL uses a three-part hybrid sort. Introsort, Quicksort and Insertion Sort. |
The C standard does not mention | this issue. |
The above is the detailed content of The difference between C qsort() and C++ sort(). For more information, please follow other related articles on the PHP Chinese website!