遗传算法与爬山算法简介_搜索算法简介

遗传算法与爬山算法简介

Not even a single day pass, when we do not have to search for something in our day to day life, car keys, books, pen, mobile charger and what not. Same is the life of a computer, there is so much data stored in it, that whenever a user asks for some data, computer has to search it's memory to look for the data and make it available to the user. And the computer has it's own techniques to search through it's memory fast, which you can learn more about in our Operating System tutorial series.

甚至没有一天的通行证,因为我们不必在日常生活中寻找某些东西,汽车钥匙,书本,笔,移动充电器等等。 就像计算机的寿命一样,其中存储了太多的数据,以至于每当用户要求某些数据时,计算机都必须搜索其内存以查找数据并将其提供给用户。 而且计算机具有自己的技术来快速搜索其内存,您可以在我们的操作系统教程系列中了解更多信息。

What if you have to write a program to search a given number in an array? How will you do it?

如果您必须编写程序来搜索数组中的给定数字怎么办? 你会怎么做?

Well, to search an element in a given array, there are two popular algorithms available:

好吧,要搜索给定数组中的元素,可以使用两种流行的算法:

  1. Linear Search

    线性搜寻

  2. Binary Search

    二进制搜索

线性搜寻 (Linear Search)

Linear search is a very basic and simple search algorithm. In Linear search, we search an element or value in a given array by traversing the array from the starting, till the desired element or value is found.

线性搜索是一种非常基本且简单的搜索算法。 在线性搜索中,我们通过从头开始遍历数组来搜索给定数组中的元素或值,直到找到所需的元素或值为止。

It compares the element to be searched with all the elements present in the array and when the element is matched successfully, it returns the index of the element in the array, else it return -1.

它将要搜索的元素与数组中存在的所有元素进行比较,并在成功匹配该元素后返回数组中该元素的索引,否则返回-1

Linear Search is applied on unsorted or unordered lists, when there are fewer elements in a list.

当列表中的元素较少时,线性搜索将应用于未排序或无序列表。

线性搜索算法的特点 (Features of Linear Search Algorithm)

  1. It is used for unsorted and unordered small list of elements.

    它用于未排序和无序的小型元素列表。

  2. It has a time complexity of O(n), which means the time is linearly dependent on the number of elements, which is not bad, but not that good too.

    它的时间复杂度为O(n) ,这意味着时间线性依赖于元素的数量,这并不坏,但也不是那么好。

  3. It has a very simple implementation.

    它有一个非常简单的实现。

We will implement the Linear Search algorithm in the next tutorial.

在下一个教程中,我们将实现线性搜索算法

二进制搜索 (Binary Search)

Binary Search is used with sorted array or list. In binary search, we follow the following steps:

二进制搜索用于排序的数组或列表。 在二进制搜索中,我们遵循以下步骤:

  1. We start by comparing the element to be searched with the element in the middle of the list/array.

    我们首先将要搜索的元素与列表/数组中间的元素进行比较。

  2. If we get a match, we return the index of the middle element.

    如果找到匹配项,则返回中间元素的索引。

  3. If we do not get a match, we check whether the element to be searched is less or greater than in value than the middle element.

    如果没有找到匹配项,则检查要搜索的元素的值是否小于或大于中间元素的值。

  4. If the element/number to be searched is greater in value than the middle number, then we pick the elements on the right side of the middle element(as the list/array is sorted, hence on the right, we will have all the numbers greater than the middle number), and start again from the step 1.

    如果要搜索的元素/数字的值大于中间数字,则我们在中间元素的右侧选择元素(因为列表/数组已排序,因此在右侧,我们将拥有所有数字大于中间数字),然后从第1步重新开始。

  5. If the element/number to be searched is lesser in value than the middle number, then we pick the elements on the left side of the middle element, and start again from the step 1.

    如果要搜索的元素/编号的值小于中间数字,则我们选择中间元素左侧的元素,然后从步骤1重新开始。

Binary Search is useful when there are large number of elements in an array and they are sorted.

当数组中有大量元素并且已对其进行排序时,二进制搜索很有用。

So a necessary condition for Binary search to work is that the list/array should be sorted.

因此,二进制搜索起作用的必要条件是列表/数组应排序。

二进制搜索功能 (Features of Binary Search)

  1. It is great to search through large sorted arrays.

    在大型排序数组中进行搜索非常好。

  2. It has a time complexity of O(log n) which is a very good time complexity. We will discuss this in details in the Binary Search tutorial.

    它的时间复杂度为O(log n) ,这是一个很好的时间复杂度。 我们将在“ 二进制搜索”教程中对此进行详细讨论。

  3. It has a simple implementation.

    它有一个简单的实现。

翻译自: https://www.studytonight.com/data-structures/search-algorithms

遗传算法与爬山算法简介

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值