JavaScript快速排序算法QuickSort实现
2016-04-14 22:10
351 查看
来自:http://blog.csdn.net/henren555/article/details/39611401
Introduction
This is a proto type method for arrays in JavaScript. It utilizes an iterative version of the Quick Sort algorithm. More information about the quick sort algorithm can be found here.
Background
I adapted an iterative version of Quick Sort made in C located here. I testedthis iterative version and a recursive version and found that the iterative version usually preforms about twice as fast. I also included some additional features that are not included in the vanilla version of JavaScript sort.
JavaScript Quick Sort Library
Collapse | Copy
Code
(function() { var defaultCompare, defaultSwap, partition, quickSort; Array.prototype.quickSort = function(compare, swap) { if (typeof compare !== "function") { compare = defaultCompare; } if (typeof swap !== "function") { swap = defaultSwap; } quickSort(this, 0, this.length - 1, compare, swap); }; defaultCompare = function(value1, value2) { return value1 < value2; }; defaultSwap = function(array, index1, index2) { var temp = array[index1]; array[index1] = array[index2]; array[index2] = temp; }; partition = function(array, start, stop, compare, swap) { var pivot = array[stop], storeIndex = start - 1; while (start < stop) { if (compare(array[start], pivot)) { storeIndex++; swap(array, storeIndex, start); } start++; } swap(array, storeIndex + 1, stop); return storeIndex + 1; }; quickSort = function(array, startIndex, stopIndex, compare, swap) { var pivot, stack, start, top; stack = new Array(stopIndex - startIndex + 1); top = -1; stack[++top] = startIndex; stack[++top] = stopIndex; while (top > -1) { stopIndex = stack[top--]; startIndex = stack[top--]; pivot = partition(array, startIndex, stopIndex, compare, swap); if (pivot - 1 > startIndex) { stack[++top] = startIndex; stack[++top] = pivot - 1; } if (pivot + 1 < stopIndex) { stack[++top] = pivot + 1; stack[++top] = stopIndex; } } }; }());
Using the libary
These examples explain how to use this code.Collapse | Copy
Code
// //Plain Sort // var plainSort = [4, 7, 6, 5, 7, 0, 1, 5, 10, 10, 8]; plainSort.quickSort(); console.log(JSON.stringify(plainSort)); //Prints: [0,1,4,5,5,6,7,7,8,10,10] // //Sort With Custom Compare Function // var descendingOrder = [4, 7, 6, 5, 7, 0, 1, 5, 10, 10, 8]; descendingOrder.quickSort(function(a, b) { //Descending order compare return a > b; }); console.log(JSON.stringify(descendingOrder)); //Prints: [10,10,8,7,7,6,5,5,4,1,0] // //Sort multiple arrays with a custom compare // var table = { "employeeName": ["Jane", "John", "Tom", "Alex", "Mary"], "id": [10, 5, 7, 3, 0] }; table.employeeName.quickSort(function(a, b) { //Strings have to be compared using the localeCompare function return (a || "").localeCompare(b) < 0; }, function(array, index1, index2) { //Sorts both arrays in the 'table' object var temp = array[index1]; array[index1] = array[index2]; array[index2] = temp; temp = table.id[index1]; table.id[index1] = table.id[index2]; table.id[index2] = temp; }); console.log(JSON.stringify(table)); //Prints:{"employeeName":["Alex","Jane","John","Mary","Tom"],"id":[3,10,5,0,7]}
相关文章推荐
- js的undefined和null的区别
- JS面向(基于)对象编程--构造方法(函数)
- 50 tips of JavaScript,这些坑你都知道吗?
- js截取字符串的两种方法substr 和 substring
- js判断为空Null与字符串为空简写方法
- 笔记练习:《Javascript入门经典(第5版)》page233_19.8Practice
- 进一步封装highchart,打造自己的图表插件:jHighChart.js
- js解析json时遇到英文单双引号
- 区分JS中的undefined,null,"",0和false
- 20160413jsp学习笔记jsp开发规范与常见标签作用
- FusionCharts简单教程(二)-----使用js加载图像和setDataXML()加载数据
- 使用js制作倒计时(含时分秒)
- 如何避免javascript命名冲突
- 【Code】JS-propertychange事件,实现实时金额计算
- js实现图片放大缩小后进行的复杂排序
- JavaScript的console.log()
- Json的问题总结
- Js实现回车响应事件案例
- JavaScript事件监听器 addEventListener
- tornado实现在JS文件的国际化和静态文件的引用