我是靠谱客的博主 幽默汉堡,最近开发中收集的这篇文章主要介绍遗传算法与爬山算法简介_搜索算法简介,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

遗传算法与爬山算法简介

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

遗传算法与爬山算法简介

最后

以上就是幽默汉堡为你收集整理的遗传算法与爬山算法简介_搜索算法简介的全部内容,希望文章能够帮你解决遗传算法与爬山算法简介_搜索算法简介所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(43)

评论列表共有 0 条评论

立即
投稿
返回
顶部