大型未排序列表上的二进制搜索或线性搜索?

编程入门 行业动态 更新时间:2024-10-15 08:23:35
本文介绍了大型未排序列表上的二进制搜索或线性搜索?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我了解二进制搜索比排序列表中的线性搜索和大型列表更有效,但是如果我们怎么办?有一个很大的列表,但是没有排序,我们使用线性搜索或二进制搜索?

解决方案

二进制搜索的概念只适用于已排序的输入。只研究它的工作原理:

I understand that the Binary search is more efficient than the Linear search in a sorted list and a large list, but what if we have a large list but not sorted, which one we use linear search or binary search?

解决方案

The concept of binary search can only work on sorted inputs. Just research how it works: Binary search at Wikipedia.

Based on your original question "binary search or linear search on unsorted lists?" the answer clearly is linear search because binary search can not be used.

However could it be possible that you at least have some knowledge of the input structure? If yes you could use that to create a better solution. If it is completely random then obviously linear search is the best. But you could easily parallelize the search like seen here: Fastest way to search for an element in unsorted array.

Let me give you a small overview for binary search. I select a random number between 1 and 100. You can now guess the number and I will tell you if my number is lower, equals or greater than your guess.

Binary search would now guess the half of the search interval, 50. I answer the guess is too high. The search interval is now from 1 to 49 and binary search now goes for 25. It repeats the search until the element is found.

If your input is unsorted then this will not work anymore because if I tell you that my element is lower than 50 then it does not necessarily mean that it is stored left to 50, it could also be to the right because the input is unsorted.

Here is an image illustrating the algorithm (found by a quick Google search):

更多推荐

大型未排序列表上的二进制搜索或线性搜索?

本文发布于:2023-11-29 11:17:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1646247.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:线性   序列   表上

发布评论

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

>www.elefans.com

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