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

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,直到对数组进行排序。
Image for post
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的变量。 它用于跟踪循环中是否交换了任何东西,如果没有任何移动,则对数组进行排序,我们可以返回结果。

Image for post

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,因为现在我们知道该数组未排序。

Image for post

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.

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

Image for post

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.

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

Image for post

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.

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

Image for post

只是这里的代码? (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排序

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值