Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What sorting method to use: quicksort, bucket sort, radix, ... for tiny data pairs? (c++)

I need to optimize some code that sorts a vector<pair<int, float >>a where the pairs needs to be sorted on the float value. The vector will have an length between 0 and 5. I've been googling and reading up on sorting methods in C++ but cannot find any benchmarks on sorting tiny data sets. For the system it's important be as fast as possible as it's used for a real time blob tracking system.

Kind regards, Pollux

like image 487
pollux Avatar asked Nov 29 '22 05:11

pollux


2 Answers

Insertion sort and Bubble sort are great for tiny data pairs.

Another option is to hard code the compare logic, with a couple if statements.
Check out What is the fastest possible way to sort an array of 7 integers? for some ideas.

like image 133
Nick Dandoulakis Avatar answered Dec 05 '22 11:12

Nick Dandoulakis


It makes no sense to read about benchmarks. You can read about and compare the complexity (Big-O) of algorithms, because it only depends on the algorithms themselves, but benchmarking is something that depends on too many factors.

You need to do the benchmarking for yourself, using the tools that you use, in the environment that matters to the users of your application.

like image 33
sbi Avatar answered Dec 05 '22 11:12

sbi