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 personally found this sorting algorithm very useful when teaching beginners about algorithms, 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 by 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 an 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)

function cocktailSort(arr) {let start = 0, end = arr.length, swapped = true;while (swapped) {swapped = false;for (let i = start; i < end - 1; i++) {if (arr[i] > arr[i+1]) {let temp = arr[i];arr[i] = arr[i+1];arr[i+1] = temp;swapped = true;}}end--;if (!swapped)break;swapped = false;for (let i = end - 1; i > start; i--) {if (arr[i - 1] > arr[i]) {let temp = arr[i];arr[i] = arr[i - 1];arr[i - 1] = temp;swapped = true;}}start++;}return arr;
}

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

function cocktailSort(arr) {//Start and end is used to keep track of where the beginning and the end of the array is at//to determine where needs to be checked for sorting//Swapped is our conditional to check if everything is sortedlet start = 0, end = arr.length, swapped = true;while (swapped) {//Setting the flag to false, in case it is true from the previous iterationswapped = false;//Bubble sort from the left side of the array to the right, moving the largest.for (let i = start; i < end - 1; i++){if (arr[i] > arr[i+1]) {let temp = arr[i];arr[i] = arr[i+1];arr[i+1] = temp;swapped = true;}}//This is to update the end, so that next iteration, we don't have to check this index.end--;//If everything is already sorted, we can break out of the loop early.if (!swapped){break;}//Setting the flag to false, so it can be used for the next phaseswapped = false;//Reverse Bubble sort, moving the smallest to the front.for (let i = end - 1; i > start; i--){if (arr[i - 1] > arr[i]){let temp = arr[i];arr[i] = arr[i - 1];arr[i - 1] = temp;swapped = true;}}//This is to update the beginning, so that next iteration, we don't have to check this index.start++;}return arr;
}

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

javascript排序


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

相关文章:

  • 从鸡尾酒会问题入门语音分离
  • 排序算法之冒泡排序及鸡尾酒排序
  • [排序]鸡尾酒排序算法实现
  • 通用内参抗体鸡尾酒套装说明书
  • 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所院校历年考研报录比汇总!
  • Python 3.14 将比 C++ 更快

javascript排序_鸡尾酒用javascript排序相关推荐

  1. javascript排序_鸡尾酒在JavaScript中排序

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

  2. 输入十个数进行排序_十大经典排序算法(Javascript描述)

    "本文13906字,阅读大约需要35分钟." 这世界上总存在着那么一些看似相似但有完全不同的东西,比如雷锋和雷峰塔,小平和小平头,玛丽和马里奥,Java和Javascript... ...

  3. 按复杂度有效性递减排序_十大经典排序算法:python源码实现,通俗深入讲解

    概述 提示:本文上万字,陆陆续续疏理知识点加测试代码,耗时近一个月.阅读时长40分钟左右. 本文将十大经典排序算法进行汇总,从源码实现.复杂度.稳定性进行分析,并对每种排序的特性进行点评.对典型算法, ...

  4. distinct 排序_自己造一个排序算法

    1.朴素的DIY排序 DIY 公式解读: rankx_table :造一个需要参考表,有两列,一列是[品牌],另一列是品牌的销量 VAR x : 获得当前上下文[品牌](指表格的行)的销量[sum o ...

  5. java sorted排序_【算法】排序算法之计数排序

    前几回,我们已经对冒泡排序.直接插入排序.希尔排序.选择排序.快速排序.归并排序.堆排序做了说明分析.本回,将对计数排序进行相关说明分析. 一.排序算法系列目录说明 冒泡排序(Bubble Sort) ...

  6. c++ 二维数组 排序_【算法】排序算法之计数排序

    前几回,我们已经对[算法]排序算法之冒泡排序.[算法]排序算法之插入排序.[算法]排序算法之希尔排序.[算法]排序算法之选择排序.[算法]排序算法之快速排序.[算法]排序算法之归并排序.[算法]排序算 ...

  7. mysql数据库表更改排序_修改SQL数据库排序规则修改表栏位排序规则

    修改SQL数据库排序规则修改表栏位排序规则 修改SQL数据库排序规则: 1.修改为单用户模式 2.然后关闭所有的查询窗口,修改Options的Collocation属性,如:Chinese_PRC_9 ...

  8. javascript 建模_如何用JavaScript编写3D建模应用程序

    javascript 建模 介绍 (Introduction) Modeling in Subsurfer is based on cubes, and every model starts as a ...

  9. java javascript数组_浅谈javascript和java中的数组

    javascript中的数组 数组的创建 直接创建方式  var str = ['java', 'js']; 使用new创建方式: var a = new Array(10);  //  定义长度为1 ...

最新文章

  1. 124页,UC伯克利大学胡戎航博士论文公布:视觉与语言推理的结构化模型
  2. MyBatis-21MyBatis高级结果映射【一对多映射(2种方式)】
  3. python 怎么查看变量的数据类型
  4. xForm应用开发手册
  5. WPF布局控件AvalonDock介绍以及应用中遇到的一个问题
  6. 01_kubernetes初始化系统和全局变量
  7. dataframe动态命名(读取不同文件并规律命名)
  8. samtools faidx输出的fai文件格式解析 | fasta转bed | fasta to bed
  9. Widows下安装SCALA
  10. 微信硬件开发系列教程04-新浪云服务器搭建(airkiss/airsync)
  11. 天天在做的数据可视化,才是企业数字化转型的关键
  12. php 过滤英文标点符号 过滤中文标点符号
  13. 微信小程序 收藏功能实现
  14. 【Vue】微信扫码支付
  15. 其中恐龙纪录片,你应该让你的孩子观看
  16. 基于java的志愿信息管理系统_java志愿者服务后台管理系统
  17. 树莓派实现USB TTL串口通信
  18. python成语接龙代码_基于Python经典版成语接龙逻辑实现
  19. java检验产品的次品率_某产品的次品率为0.1,检验员每天检验4次,每次随机地取10件产品进行检验,如发现其中的次品数多于1 - 搜题宝...
  20. C++求复数的角度_万物皆可傅里叶?复数傅里叶变换详解

热门文章

  1. 【Matlab水果识别】苹果质量检测及分级系统(带面板)【含GUI源码 1613期】
  2. jQuery 鼠标指针 悬浮在文字上提示信息
  3. 医院消防安全巡检系统
  4. VMware Workstaion-从零创建centos虚拟机
  5. Nginx负载均衡自带健康检测详解
  6. Android 使用OpenCV 进行书法字体重影比对
  7. ZeroC Ice权威指南-学习笔记1——hello world
  8. Linux环境下制作启动U盘
  9. [附源码]Python计算机毕业设计JAVA医院信息管理系统
  10. 计算机专业必须要i7处理器,懂电脑的人为什么不买i7处理器?有什么依据吗?...