搜索和排序向量的最快方法 [英] Fastest way to search and sort vectors

查看:51
本文介绍了搜索和排序向量的最快方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个项目,我需要在向量中插入数据并对它进行排序并进行搜索...

我需要尽可能快的算法来进行排序和搜索...我一直在搜索,发现std :: sort基本上是quicksort,它是最快的排序之一,但我不知道哪种搜索算法是最好的?二进制搜索?你能帮我吗?tnx ...所以我有3种方法:

  void addToVector(Obj o){fvector.push_back(o);}无效sortVector(){sort(fvector.begin(),fvector().end());}OBJ *搜索(字符串和bla){//我会在这里写二进制搜索返回binarysearch(..);} 

解决方案


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 (i.e., in the average case quick-sort is of complexity). However, quick-sort has quadratic worst-case performance (i.e., ). Furthermore, for a small number of inputs (e.g., 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: 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:

这篇关于搜索和排序向量的最快方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆