JavaScript版几种常见排序算法

1.冒泡排序(最慢)

2.选择排序

3.插入排序(比冒泡快)

4.希尔排序

5.归并排序

6.快速排序(非常快的排序方式)

function CArry(numElements){
		this.dataStore = [];
		this.gaps = [5,3,1];
		this.pos = 0;
		this.numElements = numElements;
		this.toString = toString;
		this.setData = setData;
		this.insert = insert;
		this.clear = clear;
		this.swap = swap;
		this.bubbleSort = bubbleSort;
		this.selectionSort = selectionSort;
		this.insertionSort = insertionSort;
		this.shellsort = shellsort;
		this.setGaps = setGaps;
		this.mergeSort = mergeSort;
		this.mergeArrays = mergeArrays;
        //创建长度为100的数组
		for(var i=0;i0 && (index+1)%10===0){
			 			return cur + ' ' + pre + '\n';
			 	    }else{
			 			return cur + ' ' + pre;
			 		}
			})
		}catch(e){
			console.log('数组长度为零');
		}
	};
	//insert方法()
	function insert(element){
		//this.dataStore[this.pos++] = element;
		this.dataStore[this.numElements++] = element;
	}
	//清空函数
	function clear(){
		for ( var i = 0; i < this.dataStore.length; ++i ) {
			this.dataStore[i] = 0;
		}
		//这种清空让数组全空,需要在toString里面捕获错误,而上面的是让所有数据为0,但是数组长度不变,不用捕获错误
		//this.dataStore.length = 0;    
	}
	//swap交换函数
	function swap(arr,index1,index2){
		var temp = arr[index1];
		arr[index1] = arr[index2];
		arr[index2] = temp;
	}
    //冒泡排序(最慢)
    function bubbleSort(){
    	var len = this.dataStore.length;
    	for(var i=len;i>=2;i--){
    		for(var j=0;j this.dataStore[j+1]){
    				this.swap(this.dataStore,j,j+1);
    			}
    		}
    	}
    }
    //选择排序
    function selectionSort(){
    	var min=0;
    	var len = this.dataStore.length;
    	for(var i=0;i=this.dataStore[j]){
    				min = j;
    			} 
    		}
    		this.swap(this.dataStore,i,min);
    	}
    }
    //插入排序
    function insertionSort() {
		var temp;
		var len = this.dataStore.length;
		for (var i = 1; i < len; ++i) {
			temp = this.dataStore[i];
			for(var j=i;this.dataStore[j-1]>=temp;j--){
				this.dataStore[j] = this.dataStore[j - 1];
			}
			this.dataStore[j] = temp;
		}
	}
	//希尔排序
	function shellsort() {
		for (var g = 0; g < this.gaps.length; ++g) {
			for (var i = this.gaps[g]; i < this.dataStore.length; ++i) {
				var temp = this.dataStore[i];
				for (var j = i; j >= this.gaps[g]&&this.dataStore[j-this.gaps[g]] > temp;j-=this.gaps[g]){
					this.dataStore[j] = this.dataStore[j - this.gaps[g]];
				}
				this.dataStore[j] = temp;
				console.log(this.toString())
			}
		}
	}
	//设置gaps间隔序列
	function setGaps(arr) {
		this.gaps = arr;
	}


	//归并排序的辅助函数
	function mergeArrays(arr, startLeft, stopLeft, startRight, stopRight) {
		var rightArr = new Array(stopRight - startRight + 1);
		var leftArr = new Array(stopLeft - startLeft + 1);
		k = startRight;
		for (var i = 0; i < (rightArr.length-1); ++i) {
			rightArr[i] = arr[k];
			++k;
		}
		k = startLeft;
		for (var i = 0; i < (leftArr.length-1); ++i) {
			leftArr[i] = arr[k];
			++k;
		}
		rightArr[rightArr.length-1] = Infinity; 
		leftArr[leftArr.length-1] = Infinity;
		var m = 0;
		var n = 0;
		for (var k = startLeft; k < stopRight; ++k) {
			if (leftArr[m] <= rightArr[n]) {
				arr[k] = leftArr[m];
				m++;
			}
			else {
				arr[k] = rightArr[n];
				n++;
			}
		}
		console.log("left array - ", leftArr);
		console.log("right array - ", rightArr);
	}
      //归并排序
		function mergeSort() {
			if (this.dataStore.length < 2) {
				return;
			}
			var step = 1;
			var left, right;
			while (step < this.dataStore.length) {
				left = 0;
				right = step;
				while (right + step <= this.dataStore.length) {
					mergeArrays(this.dataStore, left, left+step, right, right+step);
					left = right + step;
					right = left + step;
				}
				if (right < this.dataStore.length) {
					mergeArrays(this.dataStore, left, left+step, right, this.dataStore.length);
				}
				step *= 2;
			}
		}
	//快速排序
	function qSort(arr)	{
		if(arr.length === 0)
			return [];
		var left = [];
		var right = [];
		var pivot = arr[0];
		for(var i=1;i


你可能感兴趣的:(js数据结构与算法)