English 中文(简体)
搜索和排序 (Sōusuǒ hé páixù)
原标题:searching and sorting
  • 时间:2010-03-01 16:53:38
  •  标签:
  • search

如果列表有1024个项(lg1024 = 10),排序列表并使用二进制搜索的收益点是什么(搜索次数是多少)? 如果列表有2048个项,您的答案会如何改变? 而不是使用顺序搜索。 如果列表有1024个项(lg1024 = 10),排序列表并使用二进制搜索的收益点在搜索第11个元素时到达。 如果列表有2048个项目(lg2048 = 11),则预计在第12个搜索之后通过排序列表和使用二进制搜索来获得回报。

最佳回答

“线性查找”曲线和“二分查找”曲线相交的位置取决于访问/插入单个项需要多长时间以及有多少项。这对于每个组合的编译器、内存和CPU架构、列表中数据/节点的类型、数据值的分布、使用的排序和插入算法等都是不同的......但是,随着“足够大”的项集,尽管“大O”界限可能不精确地描述任何特定运行,它的运行时间可以通过提到它的上限如何随着增加的项数而增长来描述。

如果您能了解您要插入或搜索的具体算法,并确定组成列表访问的实际指令,以及找出执行它们所需的时钟周期等等,那么您可以精确地计算出这些。

然后,您可以确定哪个更快,以及在哪个时间点。如果您知道数据值,您可以对其进行建模。但是如果您不知道,您必须假设(例如,如果您插入的数据值已经排序,那么它会如何影响您的排序或插入函数?)

例如,单个项目检索可能需要1微秒。比较两个项目可能需要0.5微秒。将具有100个项目的排序列表插入到列表中可能需要X次检索,Y次比较和Z次更新/写入......而无序列表可能需要更多或更少取决于已有的内容以及要插入的内容。

问题回答

如果列表未排序,则需要O(n)的时间来找到它。使用快速排序进行排序的成本为O(n*log n),然后使用二分查找是O(log n)。假设x是搜索次数。 x * n = x * logn + n * logn 。通过放入不同的值,您可以估计动态。我的粗略估计表明,如果n = 1024且搜索次数大于~10,则最好先对其进行排序。将1024放入n中并尝试。





相关问题
Search field with Thickbox issue

i have a search form which is shown with Thickbox inside an iframe. the problem is.. that after i click "search" the result page is shown inside the same iframe! and i want it to be shown in the main ...

Will an incomplete google sitemap hurt my search ranking?

If I submit a sitemap.xml which does not contain all of the pages of my site, will this affect my search ranking? For example: If my sitemap only contained pages that had been created in the last ...

speeding up windows file search with C#

i made a program that search logical drives to find a specific file .if user type file name an click search button , searching begins , but i don t know how to stop searching in the middle of process....

JQuery/MVC Search Issue

I have inherited a piece of work where the entry screen shows a summary of 20 calculated variables. E.g. Var A (250), Var B (79). Clicking on any of these links takes the user to a view with a ...

Handling no results for docmd.applyfilter

I have an Access app where I use search functionality. I have a TextBox and a Search Button on the form, and it does a wildcard search of whatever the user enters in the TextBox, and displays the ...

Search by using the keyboard in a list/grid - algorithm

I need to implement a custom search in a grid and I would like to find some user interface guidelines that explain the standard way to implement it. I mean this kind of search that is initiated by ...

Embed Google/ Yahoo search into a web site or build your own

I am looking for an opinion on the whether to use Google custom search, Yahoo search builder or build my own for web projects (no more than 100 pages of content). If I should build my own - do you ...

热门标签