C++ boost::sort::spreadsort::spreadsort()














































C++ boost::sort::spreadsort::spreadsort()



Description

Spreadsort combines generic implementations of multiple high-speed sorting algorithms that outperform those in the C++ standard(std::sort) in both average and worst-case performance when there are over 1000 elements in the list to sort (for <1000 elements STL std::sort is a better choice). They are hybrids using both radix and comparison-based sorting.


Spreadsort is specialized to sorting common data types such as integer, float, and string. It has three types of sorting functions -

1) integer_sort
3) string_sort

Spreadsort checks whether the data-type provided is an integer, castable float, string, or wstring.

  • If data-type is an integer, integer_sort is used.

  • If data-type is a float, float_sort is used.

  • If data-type is a string or wstring, string_sort is used.

  • It won't accept types that don't have the above type traits.

Important Note : Spreadsort only works with Random Access Iterators.



Benchmark :   

OS: Windows

Processor: INTEL i3 6th gen

Memory: 4 GB 

No of elements = 1000000

Sorting Function

Random elements ( time required)

std::sort

992.6 ms

Boost spreadsort 

225.403 ms


No of elements = 1000

Sorting Function

Random elements ( time required)

std::sort

0.23 ms

Boost spreadsort 

0.41 ms 


To know more about Benchmarking check out the article on C++ BENCHMARKING



Header

#include<boost/sort/spreadsort/spreadsort.hpp>

Syntax
template<typename RandomAccessIter> 
boost::enable_if_c< std::numeric_limits< typename std::iterator_traits< RandomAccessIter >::value_type >::is_integer, void >::type
spreadsort(RandomAccessIter first, RandomAccessIter last);

Implementation

#include<boost/sort/spreadsort/spreadsort.hpp>
#include<iostream>
using namespace std;
int main(void)
{

vector<int> a{3,4,5,2,5,2,3};// taking random numbers in vector a

cout<<"Sorting the elements in ascending order->"<<endl;
boost::sort::spreadsort::spreadsort(a.begin(),a.end());

for(int i=0;i<a.size();i++) // printing the sorted array
cout<<a[i]<<" ";
cout<<endl;
 return 0;
}

Output :
Sorting the elements in ascending order->

2 2 3 3 4 5 5


Comments