Sorting Algorithms Query

Is there a way we can directly use sorting algorithms in our programs?
STL has sort() function which, I guess, internally implements a combination of introsort and insertion sort or something. But lets say, I want to implement bubble sort or any other standard sorting algorithm directly in my function(in the same way as sort() function) without writing the implementation code. So is there any file that can be included? or I have to implement these algorithms manually in my program?

I hope I made myself clear :) Thanks in advance.
So let me see if I got this right. You want to write a sorting function that uses iterators just like sort(), in which you want to implement your "own" sorting algorithm?
Nope. Instead , I want to use various sorting methods directly in my program without "writing" an implementation.

For eg. Instead of writing a function to determine length of string, I directly want to use strlen() function.On the similar note, I directly want to use quicksort, mergesort etc., without writing their implementation in my code. :)
You have to implement them manually. Within the C++ standard, there's no restrictions to the internal implementation of the sorting algorithm, which means, you can't know what algorithm is used for sorting. Every different compiler could have a different sorting algorithm.

Now you can implement all the sorting algorithms using iterators, and then give the function of sorting a hint about what sorting algorithm you wanna have.

Or, you can implement the sorting algorithm in different files, and then choose what sort file, i.e., implementation you wanna have in your program.

The last option is, however, so hard to implement because you're gonna use templates, and separating the source and the headers is hard with templates, especially that the keyword export is not supported anymore in C++11.
Last edited on
You have to implement them manually
[/b]you can implement all the sorting algorithms using iterators, and then give the function of sorting a hint about what sorting algorithm you wanna have[/b]

Bingo! That's exactly what I wanted to know. :) Thanks a lot for the information!
The standard algorithms library has std::sort() and std::stable_sort(). In addition, it also has the basic building blocks for implementing many common sort algorithms.

For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
template< typename RANDOM_ACCESS_ITERATOR, typename BINARY_PREDICATE >
inline void heap_sort( RANDOM_ACCESS_ITERATOR begin, RANDOM_ACCESS_ITERATOR end,
                        BINARY_PREDICATE cmp )
{
    std::make_heap( begin, end, cmp ) ;
    std::sort_heap( begin, end, cmp ) ;
}

template< typename ITERATOR, typename BINARY_PREDICATE >
inline void selection_sort( ITERATOR begin, ITERATOR end, BINARY_PREDICATE cmp )
{
    if( begin != end )
    {
        std::iter_swap( begin, std::min_element( begin, end, cmp ) ) ;
        selection_sort( ++begin, end, cmp ) ;
    }
}


The library also has:
std::partition(), std::stable_partition(), std::nth_element(), std::merge() and std::inplace_merge().

Topic archived. No new replies allowed.