查找元素是否存在于未排序数组中的最快方法?

编程入门 行业动态 更新时间:2024-10-25 06:30:32
本文介绍了查找元素是否存在于未排序数组中的最快方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

用于搜索未排序数组以进行元素线性搜索的最快算法是吗?我的意思是我想合并排序+二进制搜索的组合会更慢。还有其他选择吗? (就不涉及多线程的算法而言)?

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)?

推荐答案

是的,如果数组是未排序的,那么您就知道它的结构了那么搜索元素的最快方法就是考虑每个花费线性时间 O(n)的元素。

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).

要搜索大量数组,则可能需要考虑进行初始排序,然后将元素插入其正确的排序索引中(使用二进制搜索)。这意味着您的初始排序为 O(n log n),但是每次插入和搜索都需要 O(log n)。这都是权衡因素,它是否比 O(1)插入和 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.

更多推荐

查找元素是否存在于未排序数组中的最快方法?

本文发布于:2023-11-29 23:32:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1647963.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:序数   是否存在   组中   元素   最快

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!