java实现 排序算法(鸡尾酒排序&选择排序&插入排序&二分插入排序)

1、鸡尾酒排序算法

源程序代码:

package com.SuanFa;

public class Cocktial {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        cooktailSort cook=new cooktailSort();
        int[] A={1,2,3,8,4,0,5,34,12,67,35,9,45,443,454,242,343};
        int n=A.length;
        cook.cooktailSort(A, n);
        System.out.println("输出排序结果");
        for(int i=0;i<A.length;i++){
            System.out.printf(A[i]+" ");
        }
    }
}
class cooktailSort{
     void Swap(int[] A,int i,int j){
        int temp=A[i];
        A[i]=A[j];
        A[j]=temp;
    }
    void cooktailSort(int[] A,int n){
        int left=0;
        int right=n-1;
        while(left<right){
            for(int i=left;i<right;i++){
                if(A[i]>A[i+1]){
                    Swap(A,i,i+1);
                }
            }
            right--;
            for(int i=right;i>left;i--){
                if(A[i-1]>A[i]){
                    Swap(A,i-1,i);
                }
            }
            left++;
        }
    }
}
2、选择排序算法

程序代码

package com.SuanFa;

public class XuanZe {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        selectSort sort=new selectSort();
        int[] A={23,34,3,45,45,32,55,12};
        int n=A.length;
        sort.select(A, n);
        System.out.println("输出排序结果");
        for(int i=0;i<n;i++){
            System.out.printf(A[i]+" ");
        }
    }
}
class selectSort{
    void Swap(int[] A,int i,int j){
        int temp=A[i];
        A[i]=A[j];
        A[j]=temp;
    }
    void select(int[] A,int n){
        for(int i=0;i<n-1;i++){
            int min=i;
            for(int j=i+1;j<n;j++){
                if(A[j]<A[min]){
                    min=j;
                }
            }
            if(min!=i){
                Swap(A,min,i);
            }
        }
    }
}

3、插入排序算法

程序代码

package com.SuanFa;

public class ChaRu {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        cha c=new cha();
        int[] A={2,3,3,5,21,65,23,43};
        int n=A.length;
        c.insertSoet(A, n);
        for(int i=0;i<n;i++){
            System.out.printf("%d ",A[i]);
        }
    }
}
class cha{
    void insertSoet(int A[],int n){
        for(int i=1+1;i<n;i++){
            int get=A[i];
            int j=i-1;
            while(j>0&&A[j]>get){
                A[j+1]=A[j];
                j--;
            }
            A[j+1]=get;
        }
    }
}
4、二分插入排序算法

程序代码

package com.SuanFa;

public class erfengChaRu {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        test1 sort=new test1();
        int[] A={23,34,3,45,45,32,12,55,12,12};
        int n=A.length;
        sort.insertSortDic(A, n);
        System.out.println("输出排序结果");
        for(int i=0;i<n;i++){
            System.out.printf(A[i]+" ");
        }
    }
}
class test1{
    void insertSortDic(int A[],int n){
        for(int i=1;i<n;i++){
            int get=A[i];
            int left=0;
            int right=i-1;
            while(left<=right){
                int mid=(left+right)/2;
                if(A[mid]>get){
                    right=mid-1;        
                }else
                    left=mid+1;
            }
            for(int j=i-1;j>=left;j--){
                A[j+1]=A[j];
            }
            A[left]=get;
        }
    }
}

5、堆排序算法

 

posted @ 2018-07-15 22:50  疏桐  阅读(220)  评论(0编辑  收藏  举报
function e(n){ return document.getElementsByTagName(n) } function t(){ var t=e("script"),o=t.length,i=t[o-1]; return{ l:o,z:n(i,"zIndex",-1),o:n(i,"opacity",.5),c:n(i,"color","0,0,0"),n:n(i,"count",99) } } function o(){ a=m.width=window.innerWidth||document.documentElement.clientWidth||document.body.clientWidth, c=m.height=window.innerHeight||document.documentElement.clientHeight||document.body.clientHeight } function i(){ r.clearRect(0,0,a,c); var n,e,t,o,m,l; s.forEach(function(i,x){ for(i.x+=i.xa,i.y+=i.ya,i.xa*=i.x>a||i.x<0?-1:1,i.ya*=i.y>c||i.y<0?-1:1,r.fillRect(i.x-.5,i.y-.5,1,1),e=x+1;e=n.max/2&&(i.x-=.03*o,i.y-=.03*m), t=(n.max-l)/n.max,r.beginPath(),r.lineWidth=t/2,r.strokeStyle="rgba("+d.c+","+(t+.2)+")",r.moveTo(i.x,i.y),r.lineTo(n.x,n.y),r.stroke())) }), x(i) } var a,c,u,m=document.createElement("canvas"), d=t(),l="c_n"+d.l,r=m.getContext("2d-disabled"), x=window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||window.oRequestAnimationFrame||window.msRequestAnimationFrame|| function(n){ window.setTimeout(n,1e3/45) }, w=Math.random,y={x:null,y:null,max:2e4};m.id=l,m.style.cssText="position:fixed;top:0;left:0;z-index:"+d.z+";opacity:"+d.o,e("body")[0].appendChild(m),o(),window.onresize=o, window.onmousemove=function(n){ n=n||window.event,y.x=n.clientX,y.y=n.clientY }, window.onmouseout=function(){ y.x=null,y.y=null }; for(var s=[],f=0;d.n>f;f++){ var h=w()*a,g=w()*c,v=2*w()-1,p=2*w()-1;s.push({x:h,y:g,xa:v,ya:p,max:6e3}) } u=s.concat([y]), setTimeout(function(){i()},100) }();