javascript 算法_JavaScript中的算法

2023-11-13

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 算法

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

javascript 算法_JavaScript中的算法 的相关文章

随机推荐

  • 爬虫字体反爬的解决(一)

    爬虫字体反爬的解决 一 学习了前边的爬虫知识 大家一定爬取过很多的网站了 也一定被很多网站的各式各样的反爬机制劝退过 那么这些反爬机制如何来破解 大家也一定想破了头 本节课 我们来搞点不同寻常的有深度的事情 破解字体反爬 大家看目录 发现我
  • 【待解决】[LeetCode-101]-Symmetric Tree(判断两颗二叉树是否对称)

    文章目录 0 题目相关 1 Solution 0 题目相关 题目解读 给定两颗二叉树 对这两颗二叉树进行比较 判断这两棵二叉树是否对称 原题描述 原题链接 Given a binary tree check whether it is a
  • LeetCode题——最长无重复子串

    题目 给定一个字符串 请你找出其中不含有重复字符的 最长子串 的长度 如 输入 abcbabcd 输出 4 解释 因为无重复字符的最长子串是 abcd 所以其长度为 4 思路 一开始容易往暴力遍历的方向想 但是实际上运用窗口的思想就很容易解
  • [Unity2D/3D]实用的血条制作(第二期)

    Unity2D 3D 实用的血条制作 第二期 前言 第一期我为大家介绍了一种我自己摸索出来的血条制作方法 不是很常规 在这里我为大家介绍一种比较常用的血条制作方法 利用Mask组件来制作 让我们一起来看看叭 效果如图 1 首先我们把制作血条
  • Linux网络:数据链路层

    文章目录 数据链路层 和 网络层 认识以太网 以太网帧格式 认识MAC地址 认识MTU MTU对IP UDP TCP协议的影响 ARP协议 ARP数据报的格式 DNS Domain Name System 简介 域名简介 ICMP协议 pi
  • 【Linux】文件权限

    权限分为 r 读 w 写 x 执行 文件可以属于某个人也可以属于某个群体 由此可划分出三种 文件所有者 所属用户组 其他人 其他人指的是 既不是文件所有者且也不所属用户组中的用户 liuquan localhost ls l 总用量 0 r
  • CH8-HarmonyOS流转架构解析

    文章目录 前言 目标 核心概念 流转架构特性 Ability的调度 流转应用场景 流转架构 核心模块 跨端迁移关键流程 多端协同关键流程 分布式任务调度 连接远程PA 启动远程FA PA 迁移FA 接口IAbilityContinuatio
  • Android强大的图表开源——MPAndroidChart

    介绍 在APP开发中遇到图表的样式 一般我们要先查询GitHub上比较火的开源框架 这种图标应用广泛 统计 游戏统计 人际关系图等等 用到今天的这个框架MPAndroidChart 点击查看GitHub 一个可以拖动缩放的图表库 包含曲线图
  • 求整数的位数及各位数字之和 (15 分)

    7 5 求整数的位数及各位数字之和 15 分 对于给定的正整数N 求它的位数及其各位数字之和 输入格式 输入在一行中给出一个不超过10 9 的正整数N 输出格式 在一行中输出N的位数及其各位数字之和 中间用一个空格隔开 输入样例 321 输
  • 【Docker】Docker API的使用

    1 通过实训平台进入到操作系统界面 在 后输入vi usr lib systemd system docker service命令 进入编译模式 然后按i 小写 键 修改代码 usr bin dockerd current H tcp 0
  • 创建一维数组,长度为20,元素索引值为索引的二倍,奇数为负偶数为正,然后对数组排序

    import java util Arrays public class ArrayCreate public static void main String args int a new int 20 for int i 0 i lt 2
  • 单屏播放asf和vga文件的教学视频

    在自己的电脑上播放三分屏教学视频时 总觉得左边那两个小屏幕太占位置 还有右上方的小屏幕的播放进度条太短而无法精确拖放 虽然不是很懂HTML 但修改一下代码 还是单屏能播放的 下面是单屏播放asf和vga文件的设置 1 文件夹结构 index
  • CSS 实现七彩圆环loading动画

    前言 CSS 实现七彩圆环loading动画 速速来Get吧 1 实现效果 2 实现步骤 定义父容器宽度为 w 每个圆环之间的gap间距为 gap 圆环的border边框宽为 border root border 5px gap 30px
  • Java小练习——图书管理系统

    目录 一 图书管理系统应具备的功能 二 简单分析如何实现该系统 三 框架图 四 代码实现过程及简析 1 Book类 简析 2 BookShelf类 简析 3 IOperation接口 3 1 AddOperation类 3 2 Borrow
  • 离散事件模型

    离散事件模型通常需要用到队列和线性表 典型的例子是银行业务的模拟 本文参考的是严蔚敏的 数据结构 过程如下 用四个队列表示银行的四个窗口 用一个有序链表存储到达事件和离开事件 在初始化函数里面先初始化四个队列和一个链表 并且产生一个到达事件
  • 解决Eclipse添加新server时无法选择Tomcat7.0

    解决Eclipse添加新server时无法选择Tomcat7 0 新添加tomcat时 出现如下图情况 解决方法 这时打开工作空间目录下的 metadata plugins org eclipse core runtime settings
  • 面试题13. 机器人的运动范围(java+python)

    地上有一个m行n列的方格 从坐标 0 0 到坐标 m 1 n 1 一个机器人从坐标 0 0 的格子开始移动 它每次可以向左 右 上 下移动一格 不能移动到方格外 也不能进入行坐标和列坐标的数位之和大于k的格子 例如 当k为18时 机器人能够
  • 高级快速读入

    namespace fastIO define BUF SIZE 100000 fread gt read bool IOerror 0 inline char nc static char buf BUF SIZE p1 buf BUF
  • 考勤系统需求分析(软件工程)

    前言 随着企业人事管理的日趋复杂和企业人员的增多 企业的考勤管理变得越来越复杂 有一个比较完善的考勤管理系统显得是如此的重要 考勤管理系统是使用计算机管理方式代替以前手工处理的工作 应用计算机技术和通信技术建立一个高效率的 无差错的考勤管理
  • javascript 算法_JavaScript中的算法

    javascript 算法 The word Algorithms or Algo would impact fear in anyone who isn t really strong in maths like me Well toda