用JS实现常见排序算法

尝试用JS实现了几种常见的排序算法(插入排序、交换排序、选择排序)

//插入排序
//直接插入排序
Array.prototype.InsertSort=function(){
    let j,temp;
    for(let i =1;i<this.length;i++){
        temp=this[i];
        for(j=i-1;temp<this[j];j--){
            this[j+1]=this[j];
        }
        this[j+1]=temp;
    }
    return this
};
console.log([12,15,9,20,6,31,24,48,2,98,100,34678,2348].InsertSort());
//希尔排序,时间复杂度
Array.prototype.ShellSort=function(){
    let d,n,i,j,temp;
    n=this.length;
    for(d=Math.floor(n/2);d>=1;d=Math.floor(d/2)){
        for(i=d;i<n;i++) {
            temp = this[i];
            for (j = i - d; j >= 0 && temp < this[j]; j = j - d) {
                this[j + d] = this[j];
            }
            this[j + d] = temp;
            //console.log(this)
        }
    }
    return this;
};
console.log([12,15,9,20,6,31,24].ShellSort());

//交换排序
//冒泡排序
Array.prototype.BubbleSort= function(){
    let n,i,j,exchange=1,temp;
    n=this.length;
    for(i=0;i<n && exchange!=0;i++){
        exchange=0;
        for(j=0;j<n-i;j++){
            if(this[j]>this[j+1]){
                temp=this[j+1];
                this[j+1]=this[j];
                this[j]=temp;
                exchange=j;
            }
        }
        //console.log(this)
    }
    return this
};
console.log([50,13,55,97,27,38,49,65].BubbleSort());
//快速排序
function QuickSort(r,first,end){
    if(first<end){
        let pivot;
        pivot= Partition(r,first,end);  //一次划分
        //console.log(r);
        QuickSort(r,first,pivot-1);    //左侧递归快速排序
        QuickSort(r,pivot+1,end);      //右侧递归快速排序
    }
    //一次划分函数
    function Partition(r,first,end){
        let i=first,j=end;
        while(i<j){
            //右侧扫描
            while(i<j && r[i]<=r[j])j--;
            if(i<j){
                let temp;
                temp=r[j];
                r[j]=r[i];
                r[i]=temp;
                i++;
            }
            //左侧扫描
            while(i<j && r[i]<=r[j])i++;
            if(i<j){
                let temp;
                temp=r[j];
                r[j]=r[i];
                r[i]=temp;
                j--;
            }
        }
        return i;
    }
    return r
}
console.log(QuickSort([23,13,49,6,31,19,28],0,6));

//选择排序
//简单选择排序
Array.prototype.SelectSort=function(){
    let i,j,n,index,temp;
    n=this.length;
    for(i=0;i<n;i++){
        index=i;
        for(j=i+1;j<n;j++){
            if(this[j]<this[index])index=j;
            }
        if(index!=i){
            temp=this[i];
            this[i]=this[index];
            this[index]=temp;
        }
        //console.log(this)
    }
    return this
};
console.log([49,27,65,97,76,13,38].SelectSort());
//堆排序
Array.prototype.HeapSort=function(){
    let i,n,ii,temp;
    n=this.length;
    for(ii = this.length;ii>=1;ii--){
            this[ii]=this[ii-1]
    }
    this[0]=null;
    //console.log(this);
    for(i=n/2;i>=1;i--){
        Sift(this,i,n)
    }
    //console.log(this);
    for(i=1;i<n;i++){
        temp=this[1];
        this[1]=this[n-i+1];
        this[n-i+1]=temp;
        Sift(this,1,n-i)
    }
    this.splice(0,1);
    return this;

    function Sift(arr,k,m){
        let i=k,j=2*i,temp;
        while(j<=m){
            if(j<m && arr[j]<arr[j+1])j++;
            if(arr[i]>=arr[j]){break;}
            else{
                temp=arr[i];
                arr[i]=arr[j];
                arr[j]=temp;
                i=j;
                j=2*i;
            }

        }
    }
};
console.log([36,30,18,40,32,45,22,50].HeapSort());

猜你喜欢

转载自www.cnblogs.com/SofiaTJU/p/9348606.html