随笔 - 101  文章 - 0  评论 - 0  阅读 - 456

数据结构与算法-常用排序的封装

/**
 *
 * 各个排序算法的封装
 *
 *
 * */


function ArrayList(){
    this.array = [];

    ArrayList.prototype.insert = function (num){
        this.array.push(num);
    }

    ArrayList.prototype.toString = function (num){
        return this.array.join('->');
    }


    ArrayList.prototype.swap = function (m,n){
        let temp = this.array[m];
        this.array[m] = this.array[n];
        this.array[n] = temp;
    }



    //冒泡排序
    ArrayList.prototype.bubbleSort = function (){
        console.time('bubbleSort');
        for (let i = this.array.length - 1; i >=0 ; i--) {
            for (let j = 0; j <this.array.length - 1; j++) {
                if (this.array[j] > this.array[j+1]){
                    this.swap(j,j+1);
                }
            }
        }
        console.timeEnd('bubbleSort');
    }

    //选择排序
    ArrayList.prototype.selectSort = function (){
        console.time("selectSort")
        for (let i = 0; i < this.array.length - 1; i++) {
            let min = i;
            for (let j = min + 1; j < this.array.length; j++) {
                if (this.array[min] > this.array[j]) min = j;
            }
            this.swap(min,i);
        }
        console.timeEnd("selectSort")
    }
	
	//插入排序
	ArrayList.prototype.insertSort = function(){
		const length = this.array.length;
		console.time('insertSort')
		for (var i = 1; i <length; i++) {
			const item = this.array[i];
			let j = i;
			while(item < this.array[j - 1] && j > 0 ){
				this.array[j] = this.array[j - 1];
				j--;
			}
			this.array[j] = item;
		}
		console.timeEnd('insertSort');
	}
	
	
	//希尔排序
	/**
	 * 思路:先分组,在对每组进行插入排序
	 */
	ArrayList.prototype.xiErSort = function(){
		const length = this.array.length;
		
		let gap = Math.floor(length / 2);
		console.time('xiErSort')
		while(gap >= 1){
			for (var i = gap ; i < length; i++) {
				const item = this.array[i];
				let j = i;
				while(item < this.array[j - gap] && j > gap - 1){
					this.array[j] = this.array[j - gap];
					j -= gap;
				}
				this.array[j] = item;
			}
			gap = Math.floor(gap / 2); //更新分组间隔
		}
		console.timeEnd('xiErSort');
	}



	

	//快速排序
	
	//获取center
	ArrayList.prototype.getCenter = function(left,right){
		 let center = Math.floor((left + right) / 2);
		 if(this.array[left] > this.array[center]) this.swap(left,center);
		 if(this.array[left] > this.array[right]) this.swap(left,right);
		 if(this.array[center] > this.array[right]) this.swap(center,right);
		 
		 this.swap(center,right - 1);
		 console.log(this.array[right - 1]);
		 return this.array[right - 1];
	}
	ArrayList.prototype.quickSort = function(){
		this.quick(0, this.array.length - 1);
	}
	ArrayList.prototype.quick = function(left,right){
		if(left >= right) return;
		
		let center = this.getCenter(left,right);
		let i = left;
		let j = right - 1;
		
		while(true){
			while(this.array[++i] < center){}
			while(this.array[--j] > center){}
			if(i < j){
				this.swap(i,j);
			}else{
				break;
			}
		}
		this.swap(i,right - 1);
		
		//分而治之
		this.quick(left,i - 1);
		this.quick(i + 1,right);
	}

}

let list = new ArrayList();

list.insert(52);
list.insert(12);
list.insert(6);
list.insert(100);
list.insert(65);
list.insert(34);
list.insert(15);
list.insert(8);
list.insert(1000);
list.insert(60);
list.insert(800);
list.insert(1);
list.insert(64);
list.insert(78);
list.insert(52);
list.insert(36);
list.insert(13);
list.insert(14);

console.log(list.toString());
// list.bubbleSort(); //冒泡排序     执行时间:0.122ms左右 数据多时就慢很多
// list.selectSort(); //选择排序  执行时间:0.119ms
// list.insertSort(); //插入排序
// list.xiErSort(); //希尔排序
console.time('quickSort')
list.quickSort(); //快速排序
console.timeEnd('quickSort')
console.log(list.toString());


posted on   千里码!  阅读(3)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示