草履虫在路上

记录生活,学习的点点滴滴.致力于Web2.0学习,邮箱:caolvchong At gmail.com

2008-1-21 1:33:48

« 求某数以内的所有质数SOR算法 »

常见排序算法的javascript实现

前段考试,很久没有心思写blog,一些东西扔在了Wiki上,也没怎么打理.考完了,累得狠,典型的教育反面教材......
天亮后就准备回家了,可能blog又要很久没有东西,临走前丢点东西在这里吧.
--------------------------------------------------------------------
常见的几种排序的实现(javascript版本)
说明::
1.代码基本上是自己写的,堆排序和二路归并参考了网上的一些资料
2.转载请注明出处
3.代码以Demo的源代码为准,这里贴出的代码有可能因为高亮插件的一些原因而出现错误
--------------------------------------------------------------------
演示::
1.预览地址:Demo
2.效果图:
--------------------------------------------------------------------
核心代码:
/*===========================================================
 * 项目:常见的几种排序的实现(javascript)
 * 作者:草履虫
 * 时间:2008.1.3 -- 2008.1.4
 * 联系:caolvchong@gmail.com
 * 主页:http://cceer.xmu.edu.cn/blog/
 *===========================================================*/
/*+++++++++++++++++++++++++++++++++++++++++++++++
 *  插入排序
 * 	---直接插入排序
 * 	---折半插入排序
 * 	---shell排序
 *+++++++++++++++++++++++++++++++++++++++++++++++*/
	Array.prototype.direct_insert_sort = function(){//直接插入排序
		var i,j = this.length,k,now;
		for(i=1;i now)&& k>=0;k--){
				this[k+1] = this[k];
			}
			this[k+1] = now;
		}
	}
	Array.prototype.b_insert_sort = function(){//折半插入排序
		var i,j = this.length,k,now,low,high,middle;
		for(i=1;i>1;
				if(now < this[middle]){
					high = middle - 1;
				} else{
					low = middle + 1;
				}
			}
			for(k= i - 1;k >= high + 1;k--){
				this[k + 1] = this[k];
			}
			this[high + 1] = now;
		}
	}
	Array.prototype.shell = function(){//shell排序
		var step,i,j,k,s = this.length,value;
		for (step = s >> 1; step > 0; step >>= 1){
			for (i = 0; i < step; i++){
				for (j = i + step; j < s; j += step){
					k = j;
					value = this[j];
					while (k >= step && this[k - step] > value){//这里做的实际是直接插入排序
						this[k] = this[k - step];
						k -= step;
					}
					this[k] = value;
				}
			}
		}
	}		
/*+++++++++++++++++++++++++++++++++++++++++++++++
 * 快速排序
 * ---冒泡排序
 * ---快速排序
 *+++++++++++++++++++++++++++++++++++++++++++++++*/
	Array.prototype.bubble = function(){//冒泡排序
		var i,j,k=this.length,t;
		for(i=k-1;i>0;i--){
			for(j=0;j this[j+1]){
					t = this[j];
					this[j] = this[j+1];
					this[j+1] = t;
				}
			}
		}
	}
	Array.prototype.ex_bubble = function(){
		var i,j,k=this.length,flag,t;
		for(i=k-1;i>0;i--){
			for(j=0;j this[j+1]){
					t = this[j];
					this[j] = this[j+1];
					this[j+1] = t;
					flag = 1;
				}
			}
			if(flag == 0){
				return;
			}
		}
	}
	Array.prototype.quick_sort = function(left,right){//快速排序
		var i,j,t,key,m;
		if (left == null){
			left = 0;
		}
		if (right == null){
			right = this.length - 1;
		}
		if (left >= right){
			return;
		}
		key = this[left];
		i = left+1;
		j = right;
		do{
			while(this[i] <= key && i<=right){i++;}
			while(this[j] >= key && j > left){j--;}
			if(i < j){
				t = this[i];
				this[i] = this[j];
				this[j] = t;
			}
		}while(i> 1; k >= 0; j = k, k = (k - 1) >> 1){
                if (this[k] >= this[j]) break;
                t = this[j];
				this[j] = this[k];
				this[k] = t;
            }
        }
        for (i = s - 1; i > 0; --i){
            t = this[0];
			this[0] = this[i];
			this[i] = t;
            for (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;
				}
				t = this[j];
				this[j] = this[k];
				this[k] = t;
            }
        }
	}
/*+++++++++++++++++++++++++++++++++++++++++++++++
 * 归并排序
 * ---2-路归并排序
 *+++++++++++++++++++++++++++++++++++++++++++++++*/
	Array.prototype.merge_sort = function(left,right,arr_space){//2-路归并排序
		var i,j,k,middle;
		if (left == null){
			left = 0;
		}
		if(right == null){
			right = this.length - 1;
		}
		if(arr_space == null){
			arr_space = new Array(this.length);
		}
		if(left >= right){
			return;
		}
		middle = (left + right) >> 1;	
		this.merge_sort(left,middle,arr_space);
		this.merge_sort(middle+1,right,arr_space);	
		for(i=left,j=left,k=middle+1;i<=right;i++){
			arr_space[i] = this[(k>right || j<=middle && this[j]

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

Powered By Z-Blog 1.7 Laputa Build 70216

Copyright 2007-2008 草履虫 All Rights Reserved.