javascript排序

Just want the code? Scroll all the way down for two versions of the code:

只需要代码? 一直向下滚动以获取两个版本的代码:

  1. Without comments (just plug and use).没有评论(只需即插即用)。
  2. With comments to have a better understanding of the code.用注释可以更好地理解代码。

什么是鸡尾酒排序 (What is Cocktail Sort)

Cocktail Sort is a variation of Bubble Sort, also known as bidirectional Bubble Sort. It sort items by moving the biggest item on the list to the end, and then reverse and moves the smallest item to the beginning of the list. When compared to bubble sort, the Cocktail Sort does have better improvements but not by much.

鸡尾酒排序是气泡排序的一种变体,也称为双向气泡排序。 它通过将列表中最大的项目移动到末尾,然后反转并将最小的项目移动到列表的开头来对项目进行排序。 与气泡排序相比,“鸡尾酒排序”确实有更好的改进,但改进不多。

I personal found this sorting algorithm very useful when teaching beginners about algorithm, starting with showing them step by step on how to create a bubble sort. After making sure they understand how everything works in a bubble sort, and then asking them to remodel the code into a Cocktail sort.

我个人发现,在向初学者讲解算法时,从逐步向他们展示如何创建冒泡排序开始,这种排序算法非常有用。 确保他们了解气泡排序中的所有工作原理之后,然后要求他们将代码重塑为Cocktail排序。

鸡尾酒排序的步骤 (Steps for Cocktail Sort)

  1. Start from the beginning of the array and use bubble sort to move the largest to the end of the array.从数组的开头开始,然后使用冒泡排序将最大的数组移到数组的末尾。
  2. Start from the end of the array and move the smallest unit to the beginning of the array.从数组的末尾开始,然后将最小的单元移到数组的开头。
  3. Repeat Step 1 and 2 until the array is sorted.重复步骤1和2,直到对数组进行排序。
Wikipedia.维基百科的 GIF。

鸡尾酒在JavaScript中排序 (Cocktail Sort in JavaScript)

There are two ways to code Cocktail Sort, you use a for loop and need two integers to keep track of the beginning and the end of the array as you are pushing the smallest and the largest item into there. Then you stop once the two number is the same so that you are sure you checked and moved every single item on the list. Or you can use a while loop and check if there has been an item that got swapped. If there isn’t, you can now break out of the loop, and you are sure that everything is sorted.

有两种方法可以对Cocktail Sort进行编码,您可以使用for循环,并且需要两个整数来跟踪数组的开始和结束,因为您要将最小和最大的项目压入其中。 然后,一旦两个数字相同,便停止,以确保您检查并移动了列表中的每个项目。 或者,您可以使用while循环,检查是否有被交换的项目。 如果没有,您现在可以跳出循环,并且可以确保所有内容都已排序。

First we start with declaring a variable called swapped. It is used to keep track if anything has been swapped around within a loop, and if nothing was moved, then the array is sorted and we can return the result.

首先,我们先声明一个称为swapped的变量。 它用于跟踪循环中是否交换了任何东西,如果没有任何移动,则对数组进行排序,我们可以返回结果。

Then we want to use bubble sort and move the largest element to the end of the array. If a item was swapped, we change the boolean to true, as now we know the array is not sorted.

然后,我们要使用冒泡排序并将最大的元素移到数组的末尾。 如果交换了一个项目,则将布尔值更改为true,因为现在我们知道该数组未排序。

Now we want to check if everything is sorted, and if it is, we can break out of the loop as it is not necessary to keep going with the reverse bubble sort. Otherwise, we create another bubble sort to move the smallest element to the front of the array.

现在,我们要检查是否所有内容都已排序,如果可以,可以打破循环了,因为不必继续进行反向气泡排序。 否则,我们将创建另一个冒泡排序,以将最小的元素移到数组的前面。

That is all there is needed for the cocktail sort. Outside the while loop you can just return the array and we have a sorted array.

这就是鸡尾酒种类所需要的。 在while循环之外,您可以只返回数组,而我们有一个已排序的数组。

However, we can still optimize the code a bit. After the first bubble sort, we moved the largest element to the end. Therefore we can update the end index by subtracting one from it. So that in future iterations of the cocktail sort, we don’t need to check that index again.

但是,我们仍然可以对代码进行一些优化。 在第一个气泡排序之后,我们将最大的元素移到末尾。 因此,我们可以通过从中减去一个来更新结束索引。 这样,在以后的鸡尾酒种类迭代中,我们无需再次检查该索引。

We can also do the same to the reversed bubble sort and update the beginning of the array so we don’t have to check that in future iterations.

我们还可以对反向气泡排序进行同样的操作,并更新数组的开头,因此我们不必在以后的迭代中进行检查。

只是这里的代码? (Just here for the code?)

版本1:仅提供功能,无任何注释 (Version 1: Just the functions without any comments)

版本2:带注释 (Version 2: With comments)

翻译自: https://medium.com/@fanzhongzeng78/cocktail-sort-in-javascript-6b645c59ecea

javascript排序


http://www.taodudu.cc/news/show-3213637.html

相关文章:

  • javascript排序_鸡尾酒用javascript排序
  • 从鸡尾酒会问题入门语音分离
  • 排序算法之冒泡排序及鸡尾酒排序
  • [排序]鸡尾酒排序算法实现
  • 通用内参抗体鸡尾酒套装说明书
  • C语言实现鸡尾酒排序
  • 32位计算机最大内存怎么算,windows32位系统支持多大内存 怎么查看计算机内存
  • 32位系统为什么支持4G内存
  • 树莓派4B 4G板子64位、32位系统下使用ncnn 推理yolov4
  • 计算机32位操作系统指什么,电脑操作系统中32位和64位到底有哪些区别?
  • long类型在32位系统和64位系统的问题(android O )
  • struct结构体在32位系统和64位系统的存储空间的不同
  • 解决VirtualBox只能安装32位系统的问题
  • 32位系统与64位系统各数据类型所占空间大小
  • win10 计算机32位转64位,win1064位能改成32位吗_怎样把win10 64位转换为32位系统
  • 32位计算机能玩什么游戏,steam上32位系统的游戏 | 手游网游页游攻略大全
  • 树莓派32位系统烧录及连接
  • 32位计算机内存最大多少,32位系统到底支持多大的内存呢
  • 计算机32位操作系统最大识别到内存,win7 32位系统支持多大内存
  • 计算机32位操作系统最大识别到内存,32位系统支持多大内存
  • 32位操作系统是什么意思?
  • 32位系统和64位系统的说明
  • 关于 比笔顺
  • 像素纵横比基础知识
  • Java为啥比PHP快?
  • 信干噪比
  • 【并行算法】加速比性能模型
  • 哈夫曼树、哈夫曼编码与压缩比
  • 惯量比多少合适_伺服电机负载惯量比的合理取值汇总
  • 计算机考研大学报录比,2022考研:71所院校历年考研报录比汇总!

javascript排序_鸡尾酒在JavaScript中排序相关推荐

  1. javascript排序_鸡尾酒用javascript排序

    javascript排序 Just want the code? Scroll all the way down for two versions of the code: 只需要代码? 一直向下滚动 ...

  2. python set 排序_python set 排序_如何在Python中使用sorted()和sort()

    点击"蓝字"关注我们 ?"Python基础知识" 大卫·丰达科夫斯基  著 18财税3班 李潇潇    译 日期:2019年5月6日 一. 使用sorted() ...

  3. javascript面试_在编码面试中需要注意的3个JavaScript问题

    javascript面试 JavaScript is the official language of all modern web browsers. As such, JavaScript que ...

  4. javascript编写_如何在JavaScript中使用解构来编写更简洁,功能更强大的代码

    javascript编写 by Ashay Mandwarya ?️?? 由Ashay Mandwarya提供吗? 如何在JavaScript中使用解构来编写更简洁,功能更强大的代码 (How to ...

  5. mysql 自然排序_如何在mysql中实现自然排序

    背景 熟悉mysql的同学应该清楚,mysql在对字符串做order by排序时是按照字典序进行排序的,但是如果字符串中包含数字的话(我们称这种类型的字符串为alphanumeric),仅按照字典序的 ...

  6. java 中的排序_浅谈java中常见的排序

    浅谈java中常见的排序 学过java的人都知道,排序这一部分初次接触感觉还是有点难以理解,很多地方也会用到.然而,在java中常见的排序方法:冒泡排序,选择排序,插入排序等等.下面就让我们一起揭开他 ...

  7. java中按字母排序_如何在Java中按字母顺序对字符串进行排序

    使用toCharArray()方法 该类的toCharArray()方法将字符串转换为字符数组并返回它. 按字母顺序对字符串值进行排序获取所需的字符串. 使用toCharArray()方法将给定的字符 ...

  8. python中用def实现自动排序_用 python 实现各种排序算法

    常见集中排序的算法 归并排序 归并排序也称合并排序,是分治法的典型应用.分治思想是将每个问题分解成个个小问题,将每个小问题解决,然后合并. 具体的归并排序就是,将一组无序数按n/2递归分解成只有一个元 ...

  9. mysql查询后从高到低排序_[MySQL基础]三、排序查询

    排序查询 语法: SELECT 查询列表 FROM 表 [WHERE 筛选条件] ORDER BY 排序列表 [ASC|DESC]; #[ ]中的内容表示可选 特点:asc代表的是升序,desc代表的 ...

最新文章

  1. 一个女程序媛征男友的需求说明书
  2. vista系统 金山词霸 不取词翻译
  3. 移动web——基本知识点总结
  4. 小程序 - 数组追加兼本地存储
  5. 简单的ideatomcat热加载
  6. 为什么我从 Google 辞职而为自己工作
  7. 案例|工业物联网解决方案•工业互联网云平台
  8. android图标分组名称唯美简单可复制,扣扣分组名称简单唯美
  9. 三、OpenDaylight应用基础开发(ODL的Hello World)
  10. 系统安装部署系列教程(三):VHD方式安装系统
  11. 20.3. texi2html
  12. 耗时两周手撸了一个 RPC 轮子,是驴子是马拉出来遛遛
  13. 项目经理做什么工作的,每个公司不一样吗?
  14. python和access哪个好过计算机二级_大一考计算机二级,那考office、C语言、VB、Java、Access还是Python好呢?...
  15. 《人性的弱点》(一)
  16. 【Apple Music如何开通学生会员:使用学校邮箱辅助验证】
  17. Spotfire数据分析案例分享——几个国家的人口抚养比趋势及对比(Dependency ratio trends and compare)
  18. 地鼠的困境(最大匹配)
  19. SpringCould实战开篇
  20. 国庆集训 || Wannafly Day4

热门文章

  1. 2种常见的设备稼动率OEE监测方法
  2. spring 之 ObjectPostProcessor
  3. 喇叭原理及使用时需要避免的事项
  4. 【已解决】群晖套件中心无法添加第三方源 6.23
  5. 艾永亮:这家视频平台,就靠打造超级产品,成为年轻人的最爱
  6. [Zer0pts2020]Can you guess it?(basename漏洞)
  7. 巨量千川(放量投放)也可以玩出 ROI10 ?
  8. 互联网技术部门该如何管理
  9. Tensorflow基础教程5:循环神经网络(RNN)讲解
  10. 出租车管理系统java_Java学员作品-出租车管理系统