<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Examples</title>
<meta name="description" content="">
<meta name="keywords" content="">
<link href="" rel="stylesheet">
<script src="../js/jquery-1.11.1.js"></script>
</head>
<body>
<script>
$(function(){
var array = [1,10,50,100,45,25]
console.log(sequence.internalSort(array));
})
/**
*
* 排序的功能
*
*/
var sequence = {
internalSort : function(array){
/**
* [sequence.internalSort(array) JS内置排序 ]
* @param {[String]} array [数组元素]
*/
return array.sort(function(a,b){
return a-b;
})
},
swap : function(i, j) {
/**
* [sequence.swap(array) 交换函数 ]
* @param {[number]} i [元素1]
* @param {[number]} j [元素2]
*/
var temp = this[i];
this[i] = this[j];
this[j] = temp;
},
//冒泡排序
bubbleSort : function() {
/**
* [sequence.internalSort(array) JS内置排序 ]
* @param {[String]} array [数组元素]
*/
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);
}
},
//选择排序
selectionSort : function() {
/**
* [sequence.internalSort(array) JS内置排序 ]
* @param {[String]} array [数组元素]
*/
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);
}
},
//插入排序
insertionSort : function() {
/**
* [sequence.internalSort(array) JS内置排序 ]
* @param {[String]} array [数组元素]
*/
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;
}
},
//希尔排序(>>位运算)
shellSort : function() {
/**
* [sequence.internalSort(array) JS内置排序 ]
* @param {[String]} array [数组元素]
*/
for (var step = this.length >> 1; step > 0; step >>= 1)
{
//alert(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;
}
}
}
},
//递归快速排序
quickSort : function(s, e) {
/**
* [sequence.internalSort(array) JS内置排序 ]
* @param {[String]} array [数组元素]
*/
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);
},
//堆栈快速排序
stackQuickSort : function() {
/**
* [sequence.internalSort(array) JS内置排序 ]
* @param {[String]} array [数组元素]
*/
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);
}
},
//归并排序
mergeSort : function(s, e, b) {
/**
* [sequence.internalSort(array) JS内置排序 ]
* @param {[String]} array [数组元素]
*/
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];
},
//堆排序
heapSort : function() {
/**
* [sequence.internalSort(array) JS内置排序 ]
* @param {[String]} array [数组元素]
*/
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);
}
}
}
}
</script>
</body>
</html>