秒杀9种排序算法(JavaScript版)

1> JS原型

2> 排序中的有序区和无序区

3> 二叉树的基本知识

二：封装丑陋的原型方法

Function.prototype.method = function(name, func){
this.prototype[name] = func;
return this;
};

三：9种排序算法的思路和实现

1> 插入排序

Array.method('insertSort', function(){
var len = this.length,
i, j, tmp;
for(i=1; i<len; i++){
tmp = this[i];
j = i - 1;
while(j>=0 && tmp < this[j]){
this[j+1] = this[j];
j--;
}
this[j+1] = tmp;
}
return this;
});

2> 二分插入排序

Array.method('bInsertSort', function(){
var len = this.length,
i, j, tmp, low, high, mid;
for(i=1; i<len; i++){
tmp = this[i];
low = 0;
high = i - 1;
while(low <= high){
mid = (low+high)/2;
if(tmp < this[mid]) high = mid - 1;
else low = mid + 1;
}
for(j=i-1; j>=high+1; j--){
this[j+1] = this[j];
}
this[j+1] = tmp;
}
return this;
});

3> 希尔排序

Array.method('shellSort', function(){
var len = this.length, gap = parseInt(len/2),
i, j, tmp;
while(gap > 0){
for(i=gap; i<len; i++){
tmp = this[i];
j = i - gap;
while(j>=0 && tmp < this[j]){
this[j+gap] = this[j];
j = j - gap;
}
this[j + gap] = tmp;
}
gap = parseInt(gap/2);
}
return this;
});

4> 冒泡排序

Array.method('bubbleSort', function(){
var len = this.lenght,
i, j, tmp;
for(i=0; i<len; i++){
for(j=len-1; j>i; j--){
if(this[j] > this[j-1]){
tmp = this[j-1];
this[j-1] = this[j];
this[j] = tmp;
}
}
}
return this;
});

5> 改进的冒泡排序

Array.method('rBubbleSort', function(){
var len = this.length,
i, j, tmp, exchange;
for(i=0; i<len; i++){
exchange = 0;
for(j=len-1; j>i; j--){
if(this[j] < this[j-1]){
tmp = this[j];
this[j] = this[j-1];
this[j-1] = tmp;
exchange = 1;
}
}
if(!exchange) return this;
}
return this;
});

6> 快速排序

1) 假设第一个元素为基准元素

2) 把所有比基准元素小的记录放置在前一部分，把所有比基准元素大的记录放置在后一部分，并把基准元素放在这两部分的中间(i=j的位置)

Array.method('quickSort', function(s, t){
var i=s, j=t,
tmp;
if(s < t){
tmp = this[s];
while(i!=j){
while(j>i && this[j]>tmp) j--;//右—>左
R[i] = R[j];
while(i<j && this[j]<tmp) i++;//左—>右
R[j] = R[i];
}
R[i] = tmp;
this.quickSort(s, i-1);
this.quickSort(i+1, t);
}
return this;
});

7> 选择排序

Array.method('selectSort', function(){
var len = this.length,
i, j, k, tmp;
for(i=0; i<len; i++){
k = i;
for(j=i+1; j<len; j++){
if(this[j] < this[k]) k = j;
}
if(k!=i){
tmp = this[k];
this[k] = this[i];
this[i] = tmp;
}
}
return this;
});

8> 堆排序

1) 初始堆：

2) 堆排序：

Array.method('createHeap', function(low, high){
var i=low, j=2*i, tmp=this[i];
while(j<=high){
if(j< high && this[j]<this[j+1]) j++; //从左右子节点中选出较大的节点
if(tmp < this[j]){                    //根节点(tmp)<较大的节点
this[i] = this[j];
i = j;
j = 2*i;
}else break;
}
this[i] = tmp;                            //被筛选的元素放在最终的位置上
return this;
});

Array.method('heapSort', function(){
var i, tmp, len=this.length-1;
for(i=parseInt(len/2); i>=1; i--) this.createHeap(i, len);
for(i=len; i>=2; i--){
tmp = this[1];
this[1] = this[i];
this[i] = tmp;
this.createHeap(1, i-1);
}
return this;
});

9> 归并排序

1) 归并

2) 排序

Array.method('merge', function(low, mid, high){
var tmp = new Array(), i = low, j=mid+1, k=0;
while(i<=mid && j<=high){
if(this[i] <= this[j]){//比较第一部分和第二部分，取较小者
tmp[k] = this[i];
i++;
k++;
}else{
tmp[k] = this[j];
j++;
k++;
}
}
while(i<=mid){
tmp[k] = this[i];
i++;
k++;
}
while(j<=high){
tmp[k] = this[j];
j++;
k++;
}
for(k=0,i=low; i<=high; k++,i++) this[i] = tmp[k];

return this;
});
Array.method('mergePass', function(length, n){
var i;
for(i=0; i+2*length-1<n; i=i+2*length) this.merge(i, i+length-1, i+2*length-1);
if(i+length-1 < n) this.merge(i, i+length-1, n-1); //考虑到最后一个子表的长度可能小于length，所以要特殊处理一下

return this;
});

Array.method('mergeSort', function(){
var len = this.length,
length;
for(length=1; length<len; length=2*length) this.mergePass(length, len);

return this;
});

四：测试

var out = function(){
console.log(arguments);
}
var test = [0,1,7,6,3,4,9,8];
out(test.insertSort(), 'insertSort');
var test = [0,1,7,6,3,4,9,8];
out(test.bInsertSort(), 'bInsertSort');
var test = [0,1,7,6,3,4,9,8];
out(test.shellSort(), 'shellSort');
var test = [0,1,7,6,3,4,9,8];
out(test.bubbleSort(), 'bubbleSort');
var test = [0,1,7,6,3,4,9,8];
out(test.rBubbleSort(), 'rBubbleSort');
var test = [0,1,7,6,3,4,9,8];
out(test.quickSort(0, test.length-1), 'quickSort');
var test = [0,1,7,6,3,4,9,8];
out(test.selectSort(), 'selectSort');
var test = [0,1,7,6,3,4,9,8];
out(test.heapSort(), 'heapSort');
var test = [0,1,7,6,3,4,9,8];
out(test.mergeSort(), 'mergeSort');

posted @ 2013-10-10 11:14 JChen___ 阅读(...) 评论(...) 编辑 收藏