There is a saying:
Lei Feng knocked down Leifeng Pagoda, Java implements JavaScript.
At that time, JavaScript (originally known as LiveScript), which wanted to gain popularity by embracing Java and even changed its name, has now become brilliant. The emergence of node JS allows JavaScript to be used both on the front and back ends. Although Java still dominates the field of enterprise-level software development (C/C masters, don’t hit me...), in the world of the Web, JavaScript is unparalleled and takes the top spot.
However, in the field of traditional computer algorithms and data structures, the default language of most professional textbooks and books is Java or C/C. This has caused some trouble for me, who recently wanted to supplement the knowledge of algorithms and data structures, because I wanted to find an algorithm book with JavaScript as the default language. When I learned that there was a book in O’REILLY’s animal book series called “JavaScript Description of Data Structures and Algorithms”, I excitedly spent two days reading this book from cover to cover. It is a good introductory algorithm book for front-end developers. However, it has a big flaw, that is, there are many obvious small errors in it, which are so obvious that even a half-way programmer like me can see it at a glance. figure it out. Another problem is that many important algorithms and data structure knowledge are not mentioned in this book. These problems are simply intolerable to me as a late-stage obsessive-compulsive disorder patient. So, whenever I disagreed, I decided to look for information and summarize the algorithm myself. So, I will summarize it from the most basic knowledge point in the algorithm field-sorting algorithm.
I believe that there must be some bugs or errors or irregular grammar in the following code that I cannot find myself, so I hope you can point out the errors, because only by constantly correcting them can I Only by doing this can I make long-term progress.
One picture summary:
Overview of mainstream sorting algorithms
n: Data scale
k: The number of “buckets”
In-place: Occupies constant memory, does not occupy additional memory
Out-place: Occupies additional memory
Stability: The order of 2 equal key values after sorting is the same as before sorting The order is the same
As the simplest sort One of the algorithms, bubble sort, gives me the same feeling as when Abandon appears in a word book. It is at the first place on the first page every time, so it is the most familiar. . . There is another optimization algorithm for bubble sorting, which is to set a flag. When the elements are not exchanged during a sequence traversal, it proves that the sequence is in order. But this improvement doesn't do much to improve performance. . .
When the input data is already in positive sequence (it is already in positive sequence, what is the use of bubble sorting? .)
When the input data is in reverse order (just write a for loop to output the data in reverse order, why should you risk it? What about bubble sort, am I free...)
Bubble Sort animation demonstration algorithm Visual source: http://visualgo.net/
function bubbleSort(arr) { var len = arr.length; for (var i = 0; i arr[j+1]) { //相邻元素两两对比 var temp = arr[j+1]; //元素交换 arr[j+1] = arr[j]; arr[j] = temp; } } } return arr; }
One of the most stable sorting algorithms, because no matter what data is entered, the time complexity is O(n²). . . So when using it, the smaller the data size, the better. The only advantage may be that it does not occupy additional memory space.
Selection Sort animation demonstration Algorithm visualization source: http://visualgo.net/
function selectionSort(arr) { var len = arr.length; var minIndex, temp; for (var i = 0; i
插入排序的代码实现虽然没有冒泡排序和选择排序那么简单粗暴,但它的原理应该是最容易理解的了,因为只要打过扑克牌的人都应该能够秒懂。当然,如果你说你打扑克牌摸牌的时候从来不按牌的大小整理牌,那估计这辈子你对插入排序的算法都不会产生任何兴趣了。。。
插入排序和冒泡排序一样,也有一种优化算法,叫做拆半插入。对于这种算法,得了懒癌的我就套用教科书上的一句经典的话吧:感兴趣的同学可以在课后自行研究。。。
Insertion Sort 动图演示 算法可视化来源:http://visualgo.net/
function insertionSort(arr) { var len = arr.length; var preIndex, current; for (var i = 1; i = 0 && arr[preIndex] > current) { arr[preIndex+1] = arr[preIndex]; preIndex--; } arr[preIndex+1] = current; } return arr; }
希尔排序是插入排序的一种更高效率的实现。它与插入排序的不同之处在于,它会优先比较距离较远的元素。希尔排序的核心在于间隔序列的设定。既可以提前设定好间隔序列,也可以动态的定义间隔序列。动态定义间隔序列的算法是《算法(第4版》的合著者Robert Sedgewick提出的。在这里,我就使用了这种方法。
function shellSort(arr) { var len = arr.length, temp, gap = 1; while(gap 0; gap = Math.floor(gap/3)) { for (var i = gap; i = 0 && arr[j] > temp; j-=gap) { arr[j+gap] = arr[j]; } arr[j+gap] = temp; } } return arr; }
作为一种典型的分而治之思想的算法应用,归并排序的实现由两种方法:
在《数据结构与算法JavaScript描述》中,作者给出了自下而上的迭代方法。但是对于递归法,作者却认为:
However, it is not possible to do so in JavaScript, as the recursion goes too deep
for the language to handle.
然而,在 JavaScript 中这种方式不太可行,因为这个算法的递归深度对它来讲太深了。
说实话,我不太理解这句话。意思是JavaScript编译器内存太小,递归太深容易造成内存溢出吗?还望有大神能够指教。
和选择排序一样,归并排序的性能不受输入数据的影响,但表现比选择排序好的多,因为始终都是O(n log n)的时间复杂度。代价是需要额外的内存空间。
Merge Sort 动图演示 算法可视化来源:http://visualgo.net/
function mergeSort(arr) { //采用自上而下的递归方法 var len = arr.length; if(len =>
又是一种分而治之思想在排序算法上的典型应用。本质上来看,快速排序应该算是在冒泡排序基础上的递归分治法。
快速排序的名字起的是简单粗暴,因为一听到这个名字你就知道它存在的意义,就是快,而且效率高! 它是处理大数据最快的排序算法之一了。虽然Worst Case的时间复杂度达到了O(n²),但是人家就是优秀,在大多数情况下都比平均时间复杂度为O(n log n) 的排序算法表现要更好,可是这是为什么呢,我也不知道。。。好在我的强迫症又犯了,查了N多资料终于在《算法艺术与信息学竞赛》上找到了满意的答案:
快速排序的最坏运行情况是O(n²),比如说顺序数列的快排。但它的平摊期望时间是O(n log n) ,且O(n log n)记号中隐含的常数因子很小,比复杂度稳定等于O(n log n)的归并排序要小很多。所以,对绝大多数顺序性较弱的随机数列而言,快速排序总是优于归并排序。
Quick Sort 动图演示 算法可视化来源:http://visualgo.net/
function quickSort(arr, left, right) { var len = arr.length, partitionIndex, left = typeof left != 'number' ? 0 : left, right = typeof right != 'number' ? len - 1 : right; if (left =>
堆排序可以说是一种利用堆的概念来排序的选择排序。分为两种方法:
Heap Sort 动图演示 算法可视化来源:http://www.ee.ryerson.ca/~courses/coe428/sorting/heapsort.html
var len; //因为声明的多个函数都需要数据长度,所以把len设置成为全局变量 function buildMaxHeap(arr) { //建立大顶堆 len = arr.length; for (var i = Math.floor(len/2); i >= 0; i--) { heapify(arr, i); } } function heapify(arr, i) { //堆调整 var left = 2 * i + 1, right = 2 * i + 2, largest = i; if (left arr[largest]) { largest = left; } if (right arr[largest]) { largest = right; } if (largest != i) { swap(arr, i, largest); heapify(arr, largest); } } function swap(arr, i, j) { var temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } function heapSort(arr) { buildMaxHeap(arr); for (var i = arr.length-1; i > 0; i--) { swap(arr, 0, i); len--; heapify(arr, 0); } return arr; }
计数排序的核心在于将输入的数据值转化为键存储在额外开辟的数组空间中。
作为一种线性时间复杂度的排序,计数排序要求输入的数据必须是有确定范围的整数。
Counting Sort 动图演示 算法可视化来源:http://visualgo.net/
function countingSort(arr, maxValue) { var bucket = new Array(maxValue+1), sortedIndex = 0; arrLen = arr.length, bucketLen = maxValue + 1; for (var i = 0; i 0) { arr[sortedIndex++] = j; bucket[j]--; } } return arr; }
桶排序是计数排序的升级版。它利用了函数的映射关系,高效与否的关键就在于这个映射函数的确定。
为了使桶排序更加高效,我们需要做到这两点:
同时,对于桶中元素的排序,选择何种比较排序算法对于性能的影响至关重要。
当输入的数据可以均匀的分配到每一个桶中
当输入的数据被分配到了同一个桶中
function bucketSort(arr, bucketSize) { if (arr.length === 0) { return arr; } var i; var minValue = arr[0]; var maxValue = arr[0]; for (i = 1; i maxValue) { maxValue = arr[i]; //输入数据的最大值 } } //桶的初始化 var DEFAULT_BUCKET_SIZE = 5; //设置桶的默认数量为5 bucketSize = bucketSize || DEFAULT_BUCKET_SIZE; var bucketCount = Math.floor((maxValue - minValue) / bucketSize) + 1; var buckets = new Array(bucketCount); for (i = 0; i
基数排序有两种方法:
这三种排序算法都利用了桶的概念,但对桶的使用方法上有明显差异:
基数排序:根据键值的每位数字来分配桶
计数排序:每个桶只存储单一键值
桶排序:每个桶存储一定范围的数值
Radix Sort 动图演示 算法可视化来源:http://visualgo.net/
//LSD Radix Sort var counter = []; function radixSort(arr, maxDigit) { var mod = 10; var dev = 1; for (var i = 0; i
排序算法实在是博大精深,还有hin多hin多我没有总结到或者我自己还没弄明白的算法,仅仅是总结这十种排序算法都把我写哭了。。。
因此,以后如果我掌握了更多的排序姿势,我一定还会回来的!
推荐教程:《javascript基础教程》
The above is the detailed content of JS sorting algorithm - front-end. For more information, please follow other related articles on the PHP Chinese website!