+
low)返回一个在low和high之间的数。
复制代码
代码如下:
/****************************************
算法:split
输入:数组A[low...high]
输出:
1.若有必要,输出按上述描述的重新排列的数组A
2.划分元素A[low]的新位置w
****************************************/
function
split(array,
low,
high)
{
var
i
=
low
var
x
=
array[low]
for(var
j
=
low
+
1
j
<=
high
j++)
{
if(array[j]
<=
x)
{
i
++
if(i
!=
j)
{
var
temp
=
array[i]
array[i]
=
array[j]
array[j]
=
temp
}
}
}
temp
=
array[low]
array[low]
=
array[i]
array[i]
=
temp
return
i
}
/****************************************
算法:rquicksort
输入:A[0...n-1]
输出:按非降序排列数组A[0...n-1]
rquicksort(A,
0,
n-1)
****************************************/
function
rquicksort(array,
low,
high)
{
if(low
<
high)
{
/******随机化拆分元素的主元*******/
var
v
=
parseInt(Math.random()*(high-low+1)
+
low)
var
tmp
=
array[low]
array[low]
=
array[v]
array[v]
=
tmp
/******随机化拆分元素的主元*******/
var
w
=
split(array,
low,
high)
rquicksort(array,
low,
w
-1)
rquicksort(array,
w
+1,
high)
return
array
}
}
var
array
=
[33,
22,
11,
88,
23,
32]
array
=
rquicksort(array,
0,
array.length-1)
console.log(array)
算法的平均时间复杂度为O(nlogn) 但是当输入是已经排序的数组或几乎排好序的输入 时间复杂度却为O(n^ ) 为解决这一问题并保证平均时间复杂度为O(nlogn)的方法是引入预处理步骤 它惟一的目的是改变元素的顺序使之随机排序 这种预处理步骤可在O(n)时间内运行 能够起到同样作用的另一种简单方法是在算法中引入一个随机元素 这可以通过随机地选择拆分元素的主元来实现 随机选择主元的结果放宽了关于输入元素的所有排列的可能性相同的步骤 引入这一步来修正原先的快速排序 可得到下面所示的随机化快速排序 新算法只是在区间[low…high]中一致随机地选择一个索引v 并将A[v]和A[low]交换 然后按照原来的快速排序算法继续 这里 parseInt(Math random()*(high low+ ) + low)返回一个在low和high之间的数
/****************************************
算法 split
输入 数组A[low high]
输出
若有必要 输出按上述描述的重新排列的数组A
划分元素A[low]的新位置w
****************************************/
function split(array low high) {
var i = low
var x = array[low]
for(var j = low + j <= highj++) {
if(array[j] <= x) {
i ++
if(i != j) {
var temp = array[i]
array[i] = array[j]
array[j] = temp
}
}
}
temp = array[low]
array[low] = array[i]
array[i] = temp
return i
}
/****************************************
算法 rquicksort
输入 A[ n ]
输出 按非降序排列数组A[ n ]
rquicksort(A n )
****************************************/
function rquicksort(array low high) {
if(low <high) {
/******随机化拆分元素的主元*******/
var v = parseInt(Math random()*(high low+ ) + low)
var tmp = array[low]
array[low] = array[v]
array[v] = tmp
/******随机化拆分元素的主元*******/
var w = split(array low high)
rquicksort(array low w )
rquicksort(array w + high)
return array
}
}
var array = [ ]
array = rquicksort(array array length )
lishixinzhi/Article/program/Java/JSP/201311/20538
js常用排序实现,参考代码如下
<script>Array.prototype.swap = function(i, j)
{
var temp = this[i]
this[i] = this[j]
this[j] = temp
}
Array.prototype.bubbleSort = function()
{
for (var i = this.length - 1 i > 0 --i)
{
for (var j = 0 j < i ++j)
{
if (this[j] > this[j + 1]) this.swap(j, j + 1)
}
}
}
Array.prototype.selectionSort = function()
{
for (var i = 0 i < this.length ++i)
{
var index = i
for (var j = i + 1 j < this.length ++j)
{
if (this[j] < this[index]) index = j
}
this.swap(i, index)
}
}
Array.prototype.insertionSort = function()
{
for (var i = 1 i < this.length ++i)
{
var j = i, value = this[i]
while (j > 0 && this[j - 1] > value)
{
this[j] = this[j - 1]
--j
}
this[j] = value
}
}
Array.prototype.shellSort = function()
{
for (var step = this.length >> 1 step > 0 step >>= 1)
{
for (var i = 0 i < step ++i)
{
for (var j = i + step j < this.length j += step)
{
var k = j, value = this[j]
while (k >= step && this[k - step] > value)
{
this[k] = this[k - step]
k -= step
}
this[k] = value
}
}
}
}
Array.prototype.quickSort = function(s, e)
{
if (s == null) s = 0
if (e == null) e = this.length - 1
if (s >= e) return
this.swap((s + e) >> 1, e)
var index = s - 1
for (var i = s i <= e ++i)
{
if (this[i] <= this[e]) this.swap(i, ++index)
}
this.quickSort(s, index - 1)
this.quickSort(index + 1, e)
}
Array.prototype.stackQuickSort = function()
{
var stack = [0, this.length - 1]
while (stack.length > 0)
{
var e = stack.pop(), s = stack.pop()
if (s >= e) continue
this.swap((s + e) >> 1, e)
var index = s - 1
for (var i = s i <= e ++i)
{
if (this[i] <= this[e]) this.swap(i, ++index)
}
stack.push(s, index - 1, index + 1, e)
}
}
Array.prototype.mergeSort = function(s, e, b)
{
if (s == null) s = 0
if (e == null) e = this.length - 1
if (b == null) b = new Array(this.length)
if (s >= e) return
var m = (s + e) >> 1
this.mergeSort(s, m, b)
this.mergeSort(m + 1, e, b)
for (var i = s, j = s, k = m + 1 i <= e ++i)
{
b[i] = this[(k > e || j <= m && this[j] < this[k]) ? j++ : k++]
}
for (var i = s i <= e ++i) this[i] = b[i]
}
Array.prototype.heapSort = function()
{
for (var i = 1 i < this.length ++i)
{
for (var j = i, k = (j - 1) >> 1 k >= 0 j = k, k = (k - 1) >> 1)
{
if (this[k] >= this[j]) break
this.swap(j, k)
}
}
for (var i = this.length - 1 i > 0 --i)
{
this.swap(0, i)
for (var j = 0, k = (j + 1) << 1 k <= i j = k, k = (k + 1) << 1)
{
if (k == i || this[k] < this[k - 1]) --k
if (this[k] <= this[j]) break
this.swap(j, k)
}
}
}
function generate()
{
var max = parseInt(txtMax.value), count = parseInt(txtCount.value)
if (isNaN(max) || isNaN(count))
{
alert("个数和最大值必须是一个整数")
return
}
var array = []
for (var i = 0 i < count ++i) array.push(Math.round(Math.random() * max))
txtInput.value = array.join("\n")
txtOutput.value = ""
}
function demo(type)
{
var array = txtInput.value == "" ? [] : txtInput.value.replace().split("\n")
for (var i = 0 i < array.length ++i) array[i] = parseInt(array[i])
var t1 = new Date()
eval("array." + type + "Sort()")
var t2 = new Date()
lblTime.innerText = t2.valueOf() - t1.valueOf()
txtOutput.value = array.join("\n")
}
</script>
<body onload=generate()>
<table style="width:100%height:100%font-size:12pxfont-family:宋体">
<tr>
<td align=right>
<textarea id=txtInput readonly style="width:100pxheight:100%"></textarea>
</td>
<td width=150 align=center>
随机数个数<input id=txtCount value=500 style="width:50px"><br><br>
最大随机数<input id=txtMax value=1000 style="width:50px"><br><br>
<button onclick=generate()>重新生成</button><br><br><br><br>
耗时(毫秒):<label id=lblTime></label><br><br><br><br>
<button onclick=demo("bubble")>冒泡排序</button><br><br>
<button onclick=demo("selection")>选择排序</button><br><br>
<button onclick=demo("insertion")>插入排序</button><br><br>
<button onclick=demo("shell")>谢尔排序</button><br><br>
<button onclick=demo("quick")>快速排序(递归)</button><br><br>
<button onclick=demo("stackQuick")>快速排序(堆栈)</button><br><br>
<button onclick=demo("merge")>归并排序</button><br><br>
<button onclick=demo("heap")>堆排序</button><br><br>
</td>
<td align=left>
<textarea id=txtOutput readonly style="width:100pxheight:100%"></textarea>
</td>
</tr>
</table>
</body>