Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which sorting algorithm is used in GCC?

From cplusplus.com std::sort complexity is defined:

Complexity

Approximately N*logN comparisons on average (where N is last-first). In the worst case, up to N2, depending on specific sorting algorithm used by library implementation.

I have some limitations at running time for my apps. So i need to know if should i implement my own sorting algorithm or it would be only waste of time. They are compiled with gcc, so i need to know which sorting algorithm gcc uses.

like image 621
kravemir Avatar asked Aug 28 '11 13:08

kravemir


People also ask

What sorting algorithm does C++ use?

Internally it uses IntroSort, which is a combination of QuickSort, HeapSort and InsertionSort.

Which sort is used in STL?

sort (C++) sort is a generic function in the C++ Standard Library for doing comparison sorting. The function originated in the Standard Template Library (STL).

Which sorting algorithm is mostly used?

Quicksort. Quicksort is one of the most efficient sorting algorithms, and this makes of it one of the most used as well. The first thing to do is to select a pivot number, this number will separate the data, on its left are the numbers smaller than it and the greater numbers on the right.

Which sorting algorithm is used in Swift?

Before Swift 5, Swift's sorting algorithm was a hybrid algorithm called Introsort, which mixes the strengths of Quicksort , Heapsort and Insertion Sort into a single algorithm to guarantee a worse case of O(n log n).


1 Answers

GCC uses a variation of Musser’s introsort. This guarantees a worst-case running time of O(n log n):

It begins with quicksort and switches to heapsort when the recursion depth exceeds a level based on … the number of elements being sorted.

The implementation can be found in the stl_algo.h header in the __introsort_loop function.

like image 73
Konrad Rudolph Avatar answered Oct 12 '22 08:10

Konrad Rudolph