<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 黑夜ちつ獨行者 閱讀(414) 評論(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);
    }
    }

    ?


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


    網站導航:
     
    主站蜘蛛池模板: 中文文字幕文字幕亚洲色| 久久亚洲sm情趣捆绑调教| 最新亚洲人成网站在线观看| 成人免费午间影院在线观看| 国产亚洲sss在线播放| 最近免费中文字幕视频高清在线看 | 亚洲Av熟妇高潮30p| 午夜不卡久久精品无码免费 | 亚洲电影日韩精品| 免费人成在线观看播放a| 亚洲成a人片在线观看国产| 一级毛片正片免费视频手机看| 亚洲国产成人久久综合区| AAAAA级少妇高潮大片免费看| 亚洲色无码一区二区三区| 一级毛片免费不卡在线| 亚洲午夜久久久久久尤物| 我要看WWW免费看插插视频| 爱爱帝国亚洲一区二区三区| 久久国产成人精品国产成人亚洲| 中文字幕在线免费播放| 色拍自拍亚洲综合图区| 嫩草视频在线免费观看| 九九九国产精品成人免费视频| 亚洲夜夜欢A∨一区二区三区| 在线观看免费av网站| 亚洲熟妇无码AV| 国产L精品国产亚洲区久久| 三年片在线观看免费观看大全动漫 | 亚洲国产成人久久精品动漫| 波多野结衣免费在线观看| 色偷偷尼玛图亚洲综合| 亚洲无人区一区二区三区| 亚州免费一级毛片| 国产成人精品久久亚洲高清不卡| 亚洲无av在线中文字幕| 日韩亚洲国产高清免费视频| 亚洲免费视频一区二区三区| 亚洲天堂2016| 亚洲一区AV无码少妇电影☆| 99视频在线精品免费观看6|