<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    常見排序法

    Posted on 2006-11-10 23:03 黑夜ちつ獨行者 閱讀(420) 評論(0)  編輯  收藏

    常見排序法:

    public class Sort{
    ???public static int count=0;

    ???public boolean LT(int num1,int num2){
    ??????return num1<num2;
    ???}
    ???public void output(int[] array){
    ??????System.out.print("第"+count+"次排序:");
    ??????for(int i=0;i<array.length;i++)
    ??????System.out.print(array[i]+"??? ");
    ??????System.out.println();
    ???}

    ???//冒泡排序法
    ???public void BubbleSort(int[] array){
    ??????boolean swap=true;
    ??????int index=0;

    ??????int i=0;
    ??????while(i<array.length-1){
    ?????????int temp=array[i];
    ?????????for(int j=i;j<array.length;j++){
    ????????????if(!LT(array[i],array[j])){
    ????????????int temp2=array[i];
    ????????????array[i]=array[j];
    ????????????array[j]=temp2;
    ????????????swap=true;
    ????????????index=j;
    ????????????}else{
    ????????????swap=false;
    ????????????}
    ?????????}
    ?????????i++;
    ?????????if(swap){
    ?????????array[i]=array[index];
    ?????????array[index]=temp;
    ?????????i++;
    ?????????}
    ???output(array);
    ???}
    ???}


    //直接插入排序法
    public void InsertSort(int[] array){
    ???for(int i=1;i<array.length;++i){
    ??????if (LT(array[i],array[i-1])){
    ?????????int temp=array[i];
    ?????????array[i]=array[i-1];
    ?????????array[i-1]=temp;
    ?????????for(int j=i-1;j>0;--j){
    ????????????if(LT(array[j],array[j-1])){
    ???????????????array[j]=array[j-1];
    ???????????????array[j-1]=temp;
    ????????????}else{
    ??????????????????break;
    ????????????????????}
    ?????????}
    ???output(array);
    ???}
    }
    }


    //快速排序法
    private int Partition(int array[],int low,int high){
    ???int temp=array[low];
    ???int pivotkey=array[low];

    ???while(low<high){
    ??????while(low<high&&array[high]>pivotkey) --high;
    ??????array[low]=array[high];
    ??????while(low<high&&array[low]<=pivotkey) ++low;
    ??????array[high]=array[low];
    ???}
    ???array[low]=temp;
    ???output(array);
    ???return low;
    }


    public void QSort(int array[],int low,int high){
    ???if(low<high){
    ??????int pivotloc=Partition(array,low,high);
    ??????QSort(array,low,pivotloc-1);
    ??????QSort(array,pivotloc+1,high);
    ???}
    }


    void QuickSort(int array[]){
    ???QSort(array,0,array.length-1);
    }

    public static void main(String args[]){
    ???int array[]={49,38,65,97,76,13,27,49};
    ???Sort sort=new Sort();

    ???System.out.println("===================================");
    ???sort.output(array);
    ???System.out.println("優化冒泡排序法");
    ???sort.BubbleSort(array);

    ???System.out.println();
    ???System.out.println("===================================");
    ???array=new int[]{49,38,65,97,76,13,27,49};
    ???sort.output(array);
    ???System.out.println("直接插入排序法");
    ???sort.InsertSort(array);

    ???System.out.println();
    ???System.out.println("===================================");
    ???array=new int[]{49,38,65,97,76,13,27,49};
    ???sort.output(array);
    ???System.out.println("快速排序法");
    ???sort.QuickSort(array);
    }
    }

    ?


    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 亚洲国产一级在线观看 | 免费国产作爱视频网站| 亚洲女同成av人片在线观看| 免费看美女午夜大片| 国产一级理论免费版| 狠狠综合亚洲综合亚洲色| 免费看的黄色大片| 久久精品国产亚洲av天美18| 成人免费无码精品国产电影| 国产亚洲综合久久| 亚洲 综合 国产 欧洲 丝袜 | 亚洲第一成人影院| yellow免费网站| 国产亚洲婷婷香蕉久久精品 | 五月天婷婷精品免费视频| 国产成人亚洲精品影院| a在线观看免费视频| 亚洲AV日韩AV鸥美在线观看| 精品免费人成视频app| 亚洲中文字幕久久精品无码A| 国产美女无遮挡免费视频网站| 最新亚洲人成无码网站| 中文字幕亚洲一区二区三区| 麻豆精品不卡国产免费看| 亚洲国产成人精品无码区在线网站 | 青柠影视在线观看免费高清 | 免费一本色道久久一区| 亚洲av无码成人精品区一本二本| 亚洲高清视频一视频二视频三| 中国一级全黄的免费观看| 亚洲精品中文字幕乱码影院| 亚洲av之男人的天堂网站| 一级毛片成人免费看免费不卡 | 亚洲国产人成精品| 久久久久免费看黄a级试看| 亚洲成_人网站图片| 亚洲视频在线精品| 2021国产精品成人免费视频| 美女黄频a美女大全免费皮| 亚洲AV天天做在线观看| 永久黄网站色视频免费观看|