简体   繁体   中英

Fastest way to search and sort vectors

I'm doing a project in which i need to insert data into vectors sort it and search it ...

i need fastest possible algorithms for sort and search ... i've been searching and found out that std::sort is basically quicksort which is one of the fastest sorts but i cant figure out which search algorithm is the best ? binarysearch?? can u help me with it? tnx ... So i've got 3 methods:

void addToVector(Obj o)
{
  fvector.push_back(o);
}

void sortVector()
{
  sort(fvector.begin(), fvector().end());
}

Obj* search(string& bla)
{
 //i would write binary search here
 return binarysearch(..);
}


  • Quick-sort is one of the fastest sorting methods.

    Answer: Not quite. In general it holds (ie, in the average case quick-sort is of 在此输入图像描述 complexity). However, quick-sort has quadratic worst-case performance (ie, 在此输入图像描述 ). Furthermore, for a small number of inputs (eg, if you have a std::vector with a small numbers of elements) sorting with quick-sort tends to achieve worst performance than other sorting algorithms that are considered "slower" (see chart below):

在此输入图像描述


  • I can't figure out which searching algorithm is the best. Is it binary-search?

    Answer: Binary search has the same average and worst case performance (ie, 在此输入图像描述 ). Also have in mind that binary-search requires that the container should be arranged in ascending or descending order. However, whether is better than other searching methods (eg, linear search which has 在此输入图像描述 time complexity) depends on a number of factors. Some of them are:

    1. The number of elements/objects (see chart below).
    2. The type of elements/objects.

在此输入图像描述


Bottom Line:

For amortised O(1) access times, use a [std::unordered_map] , maybe using a custom hash for best effects.
Sorting seems to be unneccessary extra work.

Searching and Sorting efficiency is highly dependent on the type of data, the ordering of the raw data, and the quantity of the data.

For example, for small sorted data sets, a linear search may be faster than a binary search; or the time differences between the two is negligible.

Some sort algorithms will perform horribly on inversely ordered data, such a binary tree sort. Data that does not have much variation may cause a high degree of collisions on hash algorithms.

Perhaps you need to answer the bigger question: Is search or sorting the execution bottleneck in my program? Profile and find out.

If you need the fastest or the best sorting algorithm... There is no such one. At least it haven't been found yet. There are algorithms that provide better results for different data, there are algorithms that provide good results for most of data. You either need to analyze your data and find the best one for your case or use generic algo like std::sort and expect it to provide good results but not the best.

如果你的元素是整数,你应该使用桶排序算法,它运行在O(N)时间而不是O(nlogn)平均情况,如qsort [ http://en.wikipedia.org/wiki/Bucket_sort]

Sorting

In case you want to know about the fastest sorting technique for integer values in a vector then I would suggest you to refer the following link: https://github.com/fenilgmehta/Fastest-Integer-Sort

It uses radix sort and counting sort for large arrays and merge sort along with insertion sort for small arrays. According to statistics, this sorting algorithm is way faster than C++ std::sort for integral values.

It is 6 times faster than C++ STL std::sort for "int64_t array[10000000]"

Searching

If you want to know whether a particular value is present in the vector or not, then you should use binary_search(...)

If you want to know the exact location of an element, then use lower_bound(...) and upper_bound(...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM