基数排序 算法_高级排序算法简介:JS中的合并,快速和基数排序

基数排序 算法

by Yung L. Leung

梁永良

In my previous article, “The Complexity of Simple Algorithms & Data Structures in JavaScript,” we discussed simple sorting algorithms (bubble, selection & insertion sorts). Here, I go through merge, quick & radix sort, each of which has a significant improvement on average time complexity, less than O(n²).

在我之前的文章“ JavaScript中简单算法和数据结构的复杂性 ”中,我们讨论了简单的排序算法(冒泡,选择和插入排序)。 在这里,我经历了mergequickradix排序,每一个都在平均 时间复杂度上有了显着改进,小于O(n²)

Let’s go through each of these in more detail.

让我们更详细地研究每个。

合并 (Merge)

A merge sort separates a list into its individual items. It then sorts them as they are being merged into a growing ordered list.

合并排序将列表分成单独的项目。 然后,在将它们合并到一个不断增长的有序列表中时对其进行排序。

In practice, this would mean continuously slicing an array into single element arrays, before pushing each element into a larger array (the smaller value first). Every stage of pushing elements from 2 smaller arrays into 1 larger array involves determining which element from which array has the smaller value.

实际上,这将意味着在将每个元素推入更大的数组(首先是较小的值)之前,将数组连续切成单个元素的数组。 将元素从2个较小的数组推入1个较大的数组的每个阶段都涉及确定哪个数组中的哪个元素的值较小。

The complexity of merge sort is O((n log n) + 1). Remember that Big O notation (Complexity of Simple Algorithms & Data Structures in JS) is a count of the number of operations (O) with respect to the number of elements (n). So a 4 element list requires 3 splits. Note, the list is already ordered for the simplicity of the example.

合并排序的复杂度为O((n log n)+1) 。 请记住,Big O表示法( JS中简单算法和数据结构的复杂性 )是相对于元素数( n ) 的操作数( O )的计数。 因此,包含4个元素的列表需要进行3个拆分。 注意 ,为简化示例,已对列表进行排序。

Merging the 4 arrays requires 6 comparisons.

合并4个数组需要进行6个比较。

So, the mathematical calculation is as follows:

因此,数学计算如下:

For simplicity, the complexity of merge sort is O(n log n). The +1 is insignificant relative to the value of n log n and log base 2 is assumed.

为简单起见,合并排序的复杂度为O(n log n)+1相对于n log n的值微不足道,并且假定对数为2。

(Quick)

A quick sort selects a value (at index 0), swaps all lesser values closer to it, then makes a final swap to place the selected value ahead of the lesser values (an index somewhere after 0). In this manner, all values behind the pivot value are lesser values. All values ahead of it are greater values. Hence, upon pivoting, the selected value (pivot) is placed into its correct position. The process repeats until all values are “pivoted” to their correct positions.

快速排序选择一个值(在索引0处),交换所有较小的值,使其更接近该值,然后进行最后一次交换,以将所选值放在较小的值之前(索引在0之后的某个位置)。 这样, 枢轴值后面的所有值都是较小的值。 前面的所有值都是更大的值。 因此,在枢轴旋转时, 所选值( pivot )将放置在其正确位置。 重复该过程,直到将所有值“透视”到正确的位置。

Similar in practice to merge sort, quick sort requires splitting a list into smaller lists. Rather than sorting on merge, a pivot is selected to order the list such that lesser values are to its left & greater values are to its right. Therefore, it is no surprise that, like merge sort, quick sort also has a complexity of O(n log n).

在实践中与合并排序类似, 快速排序需要将列表拆分为较小的列表。 而不是对合并进行排序,而是选择一个轴对列表进行排序,以使较小的值在左侧,而较大的值在右侧。 因此,与合并排序一样快速排序也具有O(n log n)的复杂性也就不足为奇了。

So, for a 4 element array, a pivot is selected & its correct position is found (i.e., 2 at index 0 belongs at index 1). During this discovery, 3 comparisons are made with the pivot value (2) to the remaining elements (4, 1 & 3).

因此,对于4元素数组,选择一个枢轴并找到其正确位置(即,索引0处的2属于索引1)。 在此发现过程中,将枢轴值(2)与其余元素(4、1和3)进行了3个比较。

The partially sorted array (1, 2, 4, 3) is then decomposed to find the pivot positions of value 1 and 4 (by comparison to value 3), before discovering the last pivot position (value 3). This amounts to 4 comparisons and the discovery of 4 pivot positions or:

然后,在发现最后一个枢轴位置(值3 )之前,将部分排序的数组(1、2、4、3)分解以找到值14(与值3相比)的枢轴位置。 这相当于进行4次比较并发现4个关键位置,或者:

基数 (Radix)

A radix sort continuously orders a list of numbers by their base ten digit.

基数排序按数字的基数十位连续排序

In this case, the numbers (101, 54, 305, 6, 81) are first ordered by their 0’s place digit, then, 10’s place digit & finally, 100’s place digit. In practice, this means creating buckets (digits 0 to 9) for storing numbers with common digits (i.e., 101 & 81 shares a common digit at 0’s place). Then, combining all numbers in their bucket order (starting with 0’s place: 101, 81, 54, 305, 6), before repeating the process over with the 10’s place digits. This continues until the highest placed digits are reached (i.e., 101 & 305 have 100’s place digits).

在这种情况下,数字(101、54、305、6、81)首先按其0的位置数字排序,然后按10的位置数字排序,最后按100的位置数字排序。 实际上,这意味着创建存储区(数字0至9)来存储具有公共数字的数字(即10 1和8 1在0位置共享一个公共数字)。 然后,在其桶顺序组合的所有数字(从0开始的地方:10 1,8 1,5 4,30 5,6),重复该过程在与所述10个位数字之前。 这将继续直到达到最高的位数(即1 01和3 05具有100的位数)。

In general, the complexity of the radix sort is O(kn).

通常, 基数排序的复杂度为O(kn)

  • n is the number of elements

    n是元素数

  • k is the average number of digits per element

    k是每个元素的平均位数

The quantity of numbers to sort (n) is the number of times it is required to make deposits into these digit buckets. So the list of 101, 54, 305, 6, 81 requires at least 5 deposits. The higher the digits (k) of the number collection, the more times it is required to repeat the sorting process from 0’s, 10’s, 100’s, 1000’s, etc. So the list 101, 54, 305, 6, 81 requires 5 deposits for 0’s, 10’s & 100’s place. That’s a total of 3 x 5 = 15 deposits.

要排序的数字数量( n )是向这些数字桶中进行存款所需的次数。 因此,清单101、54、305、6、81至少需要5笔存款。 数字集合的数字( k )越高,从0,10,100,1000等重复排序过程所需的次数就越多。因此,列表101、54、305、6、81需要5个存放项0时,10 米的100的地方。 总共有3 x 5 = 15个存款。

结论 (Conclusion)

Learning advanced algorithms does not diminish the importance of the more basic ones. It is through the studying of basic algorithms that you learn about what it means to search or sort, simply. And from this study, you can begin to understand the problems that come with these basic algorithms.

学习高级算法并不会削弱更基本算法的重要性。 通过学习基本算法,您可以简单地了解搜索或排序的含义。 从这项研究中,您可以开始理解这些基本算法带来的问题。

Nothing is created in a vacuum. It starts with an idea. Where it goes from there is limited by the human mind and what we can do with the physical world around us. It is “always day one,” should you choose to expand your horizons.

真空中什么也不会创造。 它始于一个想法。 它的发源地受到人类思想以及我们对周围自然世界的限制。 如果您选择扩大视野,那将是“永远的第一天”。

参考: (Reference:)

https://www.udemy.com/js-algorithms-and-data-structures-masterclass/

https://www.udemy.com/js-algorithms-and-data-structures-masterclass/

翻译自: https://www.freecodecamp.org/news/an-intro-to-advanced-sorting-algorithms-merge-quick-radix-sort-in-javascript-b65842194597/

基数排序 算法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值