Home > Web Front-end > JS Tutorial > js常用排序实现代码_javascript技巧

js常用排序实现代码_javascript技巧

WBOY
Release: 2016-05-16 18:13:16
Original
904 people have browsed it
复制代码 代码如下:

<script> <BR>Array.prototype.swap = function(i, j) <BR>{ <BR>var temp = this[i]; <BR>this[i] = this[j]; <BR>this[j] = temp; <BR>} <br><br>Array.prototype.bubbleSort = function() <BR>{ <BR>for (var i = this.length - 1; i > 0; --i) <BR>{ <BR>for (var j = 0; j < i; ++j) <BR>{ <BR>if (this[j] > this[j + 1]) this.swap(j, j + 1); <BR>} <BR>} <BR>} <br><br>Array.prototype.selectionSort = function() <BR>{ <BR>for (var i = 0; i < this.length; ++i) <BR>{ <BR>var index = i; <BR>for (var j = i + 1; j < this.length; ++j) <BR>{ <BR>if (this[j] < this[index]) index = j; <BR>} <BR>this.swap(i, index); <BR>} <BR>} <br><br>Array.prototype.insertionSort = function() <BR>{ <BR>for (var i = 1; i < this.length; ++i) <BR>{ <BR>var j = i, value = this[i]; <BR>while (j > 0 && this[j - 1] > value) <BR>{ <BR>this[j] = this[j - 1]; <BR>--j; <BR>} <BR>this[j] = value; <BR>} <BR>} <br><br>Array.prototype.shellSort = function() <BR>{ <BR>for (var step = this.length >> 1; step > 0; step >>= 1) <BR>{ <BR>for (var i = 0; i < step; ++i) <BR>{ <BR>for (var j = i + step; j < this.length; j += step) <BR>{ <BR>var k = j, value = this[j]; <BR>while (k >= step && this[k - step] > value) <BR>{ <BR>this[k] = this[k - step]; <BR>k -= step; <BR>} <BR>this[k] = value; <BR>} <BR>} <BR>} <BR>} <br><br>Array.prototype.quickSort = function(s, e) <BR>{ <BR>if (s == null) s = 0; <BR>if (e == null) e = this.length - 1; <BR>if (s >= e) return; <BR>this.swap((s + e) >> 1, e); <BR>var index = s - 1; <BR>for (var i = s; i <= e; ++i) <BR>{ <BR>if (this[i] <= this[e]) this.swap(i, ++index); <BR>} <BR>this.quickSort(s, index - 1); <BR>this.quickSort(index + 1, e); <BR>} <br><br>Array.prototype.stackQuickSort = function() <BR>{ <BR>var stack = [0, this.length - 1]; <BR>while (stack.length > 0) <BR>{ <BR>var e = stack.pop(), s = stack.pop(); <BR>if (s >= e) continue; <BR>this.swap((s + e) >> 1, e); <BR>var index = s - 1; <BR>for (var i = s; i <= e; ++i) <BR>{ <BR>if (this[i] <= this[e]) this.swap(i, ++index); <BR>} <BR>stack.push(s, index - 1, index + 1, e); <BR>} <BR>} <br><br>Array.prototype.mergeSort = function(s, e, b) <BR>{ <BR>if (s == null) s = 0; <BR>if (e == null) e = this.length - 1; <BR>if (b == null) b = new Array(this.length); <BR>if (s >= e) return; <BR>var m = (s + e) >> 1; <BR>this.mergeSort(s, m, b); <BR>this.mergeSort(m + 1, e, b); <BR>for (var i = s, j = s, k = m + 1; i <= e; ++i) <BR>{ <BR>b[i] = this[(k > e || j <= m && this[j] < this[k]) ? j++ : k++]; <BR>} <BR>for (var i = s; i <= e; ++i) this[i] = b[i]; <BR>} <br><br>Array.prototype.heapSort = function() <BR>{ <BR>for (var i = 1; i < this.length; ++i) <BR>{ <BR>for (var j = i, k = (j - 1) >> 1; k >= 0; j = k, k = (k - 1) >> 1) <BR>{ <BR>if (this[k] >= this[j]) break; <BR>this.swap(j, k); <BR>} <BR>} <BR>for (var i = this.length - 1; i > 0; --i) <BR>{ <BR>this.swap(0, i); <BR>for (var j = 0, k = (j + 1) << 1; k <= i; j = k, k = (k + 1) << 1) <BR>{ <BR>if (k == i || this[k] < this[k - 1]) --k; <BR>if (this[k] <= this[j]) break; <BR>this.swap(j, k); <BR>} <BR>} <BR>} <br><br>function generate() <BR>{ <BR>var max = parseInt(txtMax.value), count = parseInt(txtCount.value); <BR>if (isNaN(max) || isNaN(count)) <BR>{ <BR>alert("个数和最大值必须是一个整数"); <BR>return; <BR>} <BR>var array = []; <BR>for (var i = 0; i < count; ++i) array.push(Math.round(Math.random() * max)); <BR>txtInput.value = array.join("\n"); <BR>txtOutput.value = ""; <BR>} <br><br>function demo(type) <BR>{ <BR>var array = txtInput.value == "" ? [] : txtInput.value.replace().split("\n"); <BR>for (var i = 0; i < array.length; ++i) array[i] = parseInt(array[i]); <BR>var t1 = new Date(); <BR>eval("array." + type + "Sort()"); <BR>var t2 = new Date(); <BR>lblTime.innerText = t2.valueOf() - t1.valueOf(); <BR>txtOutput.value = array.join("\n"); <BR>} <BR></script>











随机数个数


最大随机数







耗时(毫秒):
































Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template