javascript 算法_JavaScript中的算法

javascript 算法

The word “Algorithms” or “Algo” would impact fear in anyone who isn’t really strong in maths like me. Well today i’m not here to impact fear into anyone, and i’m not also here to dispute the fact that Algorithms isn’t hard. Today i am going to discuss easier ways of understanding some sorting methods in Algorithms which includes: “INSERTION SORT METHOD” and the “MERGE SORT METHOD” a.k.a “DIVIDE AND CONQUER METHOD”.

“算法”或“算法”一词会对像我这样在数学上并不很强的人产生恐惧。 好了,今天我不是在这里将恐惧带给任何人,我也不是在这里也质疑算法并不难的事实。 今天,我将讨论一些更容易理解算法中排序方法的方法,这些方法包括: “插入排序方法”“合并排序方法”,又称为“分而治之方法”。

大纲 (OUTLINE)

What is an Algorithm?

什么是算法

Why are Algorithms even important?

为什么算法如此重要?

INSERTION and MERGE SORT algorithm methods in JavaScript.

JavaScript中的INSERTIONMERGE SORT算法方法。

前提条件 (PREREQUISITE)

A basic understanding of JavaScript data structures, and Iterating methods.

对JavaScript数据结构和迭代方法有基本的了解。

A fundamental knowledge of Algorithms and Data structures.

基本的算法和数据结构知识。

Definitely a Programming experience :)

绝对的编程经验:)

什么是算法? (What is an Algorithm?)

Algorithmsalgorithms algorithms…

算法算法算法…

Simply put, a well-defined computational-procedure that involves taking a set of input as values, and producing a set of values as output is known as an Algorithm. Let’s break it even further, a defined procedure/process that involves taking an input and giving out an output is an algorithm.

简而言之,将包含一组输入作为值并生成一组值作为输出的定义明确的计算过程称为算法。 让我们进一步打破它,一个涉及输入和给出输出的已定义过程/过程是一种算法。

Algorithms has now evolved more than being a procedure that takes an input and gives out an output, it is now more recognized as a tool for solving well-defined computational problems.

现在,算法的发展已不仅仅是一个需要输入并给出输出的过程, 现在 已被广泛认为是 解决定义明确的计算问题 工具

Understanding algorithms is a skill every human being, software professional could have but might not know. Let me explain quite further, the Human Brain works in ways we might not even understand, but when it comes to selecting the best out of the worse things, there you go… Your Brain creates it’s own Algorithm.

理解算法是每个人,软件专业人员都可以但不知道的一项技能。 让我进一步解释一下, 人脑的工作方式可能我们甚至还不了解,但是当要从最坏的情况中选择最好的东西时,就可以了…… 您的大脑创建了自己的算法。

To illustration: Suppose you went to shop for some oranges and on getting home you discovered that some were bad, well your brain does the sorting process and then you filter the shopping bag to contain only the good oranges left and probably return the bad ones.

举例说明:假设您去商店买了一些橘子,回家后发现其中一些不好 ,那么大脑会进行分类 ,然后过滤购物袋,只保留剩下的好橘子,然后将坏橘子退还。

为什么算法很重要? (WHY ARE ALGORITHMS IMPORTANT?)

Algorithms!!! Why are they even important?

算法!!! 为什么它们甚至很重要

Algorithms are very important because the world works on Algorithms. You might be amazed at that, but it’s the fact. Almost everything in the real world involve algorithms just in-case you didn’t know. Computers, Machines, Applications, Languages were all built on Algorithms. So knowing how Algorithms work doesn’t only make you a better software developer, but it also allows you to understand how the world really works.

算法非常重要,因为世界都在研究算法。 您可能对此感到惊讶,但这是事实。 现实世界中 几乎所有事情都涉及算法,以防万一您不知道。 计算机,机器,应用程序,语言都基于算法构建。 因此,了解算法的工作原理不仅可以使您成为一名更好的软件开发人员,还可以使您了解整个世界的真实情况。 作品。

插入排序方法 (INSERTION SORT METHOD)

At last, our first algorithm method. The INSERTION method is one of the methods in algorithm sorting methods. Let us take note of the keyword sorting, we do this everyday but we might not even realize that there are better ways of implementing the sort method. Well in Algorithm there is a way to identify if a particular method is quite efficient. But how? We measure the Speed(secs) it takes for a specific method to run as the number of inputs grows.

最后,我们的第一个算法方法。 INSERTION方法是算法排序方法中的一种方法。 让我们记下关键字排序,我们每天都这样做,但是我们甚至可能没有意识到有更好的方法来实现排序方法。 在算法中,有一种方法可以识别特定方法是否非常有效。 但是如何? 我们测量随着输入数量的增加,特定方法运行所需的Speed( secs )

The Insertion sort method is a very familiar way of sorting things in the real world. Suppose we have a deck of shuffled cards spread across a table with the numbers on the card showed to us, the way we sort the cards from the lowest number to the highest number, is very similar to the insertion sort method. You start by putting the card with the lowest number in the first set, if only the next card in the set is greater than the last card, based on the number on it.

插入排序方法是在现实世界中对事物进行排序的一种非常熟悉的方法。 假设我们有一叠洗牌,分布在一张桌子上,卡片上显示的数字对我们显示,从最低编号到最高编号的排序方法与插入排序方法非常相似。 首先,将编号最低的卡放在第一组中(如果该组中只有下一张卡大于最后一张卡的话)。

Let me quickly explain this using a code sample. The code sample will be written in JavaScript, but do well to note that Algorithms can be implemented in any Programming Language of your choice.

让我使用代码示例快速解释这一点。 该代码示例将使用JavaScript编写,但请注意,可以使用您选择的任何编程语言来实现算法

The preceding code sample shows a step-by-step process on the INSERTION sort method works. The Data Structure used here is an Array. An Array is a special kind of data structure that is used to store values as elements, and the elements are accessed by it’s index.

前面的代码示例显示了有关INSERTION排序方法工作的分步过程。 这里使用的数据结构是一个Array数组是一种特殊的数据结构,用于将值存储为元素,并且元素通过其索引进行访问。

In the line 1 of the code, we initialized an Array that contained unordered set of numbers. Then we used a for-loop(2) to iterate through the elements in the Array by it’s index and length property and then check if (5)the second element in the array is less than the first element, if the condition is true we swap the elements in the array to fit in an ascending order, but if the condition is false, we set the element to be in it’s original position(9).

在代码的第1行 ,我们初始化包含无序组数字的Array。 然后,我们使用一个for-loop(2)遍历数组中indexlength属性 然后检查(5)数组中的第二个元素是否小于第一个元素,如果条件为true,我们将数组中的元素交换为升序排列,但是如果条件为false,则将元素设置为处于其原始位置( 9 )。

If you have to change the format to a descending order, you could try that by changing the condition symbol in line 5 to (<). That is:

如果必须将格式更改为降序,则可以尝试通过将第5行中的条件符号更改为(<)来进行尝试。 那是:

5   while(j > -1 && numbers[j] < key) //Descending order

So there we have it, an algorithm method that is used to sort a given set of inputs in an Ascending order. Pretty clear and straight-forward. This algorithm is quite efficient. Although i will discuss ways in which we can know if a specific algorithm method is efficient in another Article. But for the time-being, you can keep it in mind that the “MERGE SORT” is more efficient than the INSERTION SORT as the number of inputs in an “Array grows” or increases.

这样便有了一种算法方法,该方法用于按升序对给定的一组输入进行排序。 相当清晰 直接 。 该算法非常有效。 尽管我将在另一篇文章中讨论我们可以了解特定算法方法是否有效的方法。 但是暂时,您要记住,随着“ 数组”中输入的数量增加或增加 ,“ MERGE SORT ”比“ INSERTTION SORT ”更有效。

合并排序方法(分而治之) (MERGE SORT METHOD (DIVIDE AND CONQUER))

Oh yeah!! We made it to the final method for today. The MERGE METHOD is one of the best methods i have ever seen in SORT ALGORITHMS. This method even has it’s own principle attached to it, just to show how unique and interesting it is. The “DIVIDE AND CONQUERprinciple.

哦耶!! 今天我们已经达到了最终方法。 合并方法是我在排序算法中见过的最好的方法之一 这种方法甚至附加了自己的原理 ,只是为了说明它的独特性和趣味性。 “ 分而治之 ”的原则

This principle is quite easy to understand, because just as the name implies, you DIVIDE the PROBLEM into smaller bits, then SOLVE those smaller bits into reliable SOLUTIONS, after that SUM all those bits as one, and you have CONQUERED the problem.

这个道理很容易理解,因为正如名字所暗示的,你 问题分解成更小的位,那么解决这些小位到可靠的解决方案 ,即SUM所有这些位为一体,并且已经征服了问题之后。

To illustrate, let us consider the following:

为了说明,让我们考虑以下内容:

//An array of 14 elements
let numbers = [10,5,4,2,9,6,8,7,11,14,13,1,12,3];
//Divide this array into smaller bits
[10,5,4,2,9,6,8] [7,11,14,13,1,12,3]
[10,5,4] [2,9,6] [8] [7,11,14] [13,1,12] [3]//keep dividing
[10,5] [4] [2,9] [6] [8] [7,11] [14] [13,1] [12] [3] //...
//Keep dividing to it's smallest parts
[10] [5] [4] [2] [9] [6] [8] [7] [11] [14] [13] [1] [12] [3]
//Then sorts the smallest parts
[1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14]
//And Sum it up
[1,2,3,4,5,6,7,8,9,10,11,12,13,14]//=> Finally Conquered it...

Let us quickly see a code sample that explains how this all works:

让我们快速查看一个代码示例 ,该代码示例说明了这一切的工作方式

结论 (CONCLUSION)

The MERGE METHOD is also another useful way of implementing the sort Algorithm and it also seems to be more efficient than the INSERTION method. Let me not bug you with efficiency for now.

MERGE方法也是实现排序算法的另一种有用方法,并且它似乎比INSERTION方法更有效。 现在让我不要为效率烦恼。

Later on , i will publish an Article on “How to get the efficiency of an Algorithm and why we need to know how efficient an Algorithm method is”. Until then, i hope i have been able to enlighten you my distinguished audience.

稍后,我将发表一篇文章如何获得算法的效率以及为什么我们需要知道算法方法的效率 ”。 在此之前 ,我希望我能启发您尊敬的听众。

Thank you for reading my article. Here at my blog or medium I regularly write about backend development, digital marketing and content management system. To read my future posts simply join my publication or click ‘Follow’ Also feel free to connect with me via Twitter, Facebook, Instagram.

感谢您阅读我的文章。 在这里,我会定期在博客媒体上撰写有关后端开发,数字营销和内容管理系统的文章。 要阅读我将来的帖子,只需加入我的出版物或单击“关注”,还可以通过TwitterFacebookInstagram与我联系。

If you are interested in backend development (or you’re internet enthusiast) both (Mobile | Web | Desktop) videos subscribe to my Youtube channel, we will be posting a collection of help full tutorials and guides like this one for artisans.

如果您对后端开发感兴趣(或者您是互联网狂热者),两个(移动| Web |桌面)视频都订阅了我的 Youtube频道 ,我们将为工匠发布一系列此类的完整帮助指南和指南

If you enjoy this post, make sure to let us know and share it with your friends and subscribe to my growing channel.

如果您喜欢这篇文章,请确保让我们知道并与您的朋友分享,并订阅我成长中的频道

Sharing is caring.

分享是关怀。

翻译自: https://medium.com/backenders-club/algorithms-in-javascript-3d898695a50b

javascript 算法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值