javascript排序
Just want the code? Scroll all the way down for two versions of the code:
只需要代码? 一直向下滚动以获取两个版本的代码:
- Without comments (just plug and use). 没有评论(只需即插即用)。
- 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)
- Start from the beginning of the array and use bubble sort to move the largest to the end of the array. 从数组的开头开始,然后使用冒泡排序将最大的数组移到数组的末尾。
- Start from the end of the array and move the smallest unit to the beginning of the array. 从数组的末尾开始,然后将最小的单元移到数组的开头。
- Repeat Step 1 and 2 until the array is sorted. 重复步骤1和2,直到对数组进行排序。
![Image for post](https://i-blog.csdnimg.cn/blog_migrate/dd6aa9c0bebe6c728574174c7ed31085.png)
鸡尾酒在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](https://i-blog.csdnimg.cn/blog_migrate/74650570e282cbab33ef6da5f58d7648.png)
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](https://i-blog.csdnimg.cn/blog_migrate/b5df4cc9f964ecdb4766b79bc0a8c318.png)
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](https://i-blog.csdnimg.cn/blog_migrate/bdd87c8b3093f98d625ae7844765db07.png)
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](https://i-blog.csdnimg.cn/blog_migrate/8965dd4b9367f9b1158306392e7c06b3.png)
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](https://i-blog.csdnimg.cn/blog_migrate/ad8df7585892231752bc98d3ccdb00e2.png)
只是这里的代码? (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排序