Fastest way to find if element exists in an unsorted array?

自古美人都是妖i 提交于 2019-12-13 08:38:56

问题


Is the fastest algorithm for searching an unsorted array for an element linear search? I mean I guess a combination of merge sort + binary search would be slower. Are there any other options? (in terms of algorithms not involving multithreading)?


回答1:


Yes, if the array is unsorted and that's all you know about its structure then the fastest way to search for an element is to consider every one which takes linear time O(n).

If you are going to be searching the array a lot then you may want to consider an initial sort and then insert elements into their correct sorted index (using binary search). This means that you have your initial sort as O(n log n) but each insert and search takes O(log n). It's all about tradeoffs and whether that is better than O(1) insert and O(n) search.

You said no multithreading but that is an easy way to boost performance, have multiple threads look at different chunks in the list.



来源:https://stackoverflow.com/questions/15471511/fastest-way-to-find-if-element-exists-in-an-unsorted-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!