<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);
    }
    }

    ?


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


    網站導航:
     
    主站蜘蛛池模板: 在线免费中文字幕| 毛片免费全部播放无码| 免费一级毛片免费播放| 久久综合亚洲色hezyo| 毛片免费观看的视频在线| 亚洲最大的成人网| 在线免费观看一级片| 免费人成再在线观看网站| 亚洲高清视频一视频二视频三| 污网站在线免费观看| 亚洲一区二区三区乱码A| 久久精品无码免费不卡| 久久夜色精品国产嚕嚕亚洲av| 免费A级毛片在线播放| 亚洲专区中文字幕| 日本大片在线看黄a∨免费| 午夜在线免费视频| 亚洲av之男人的天堂网站| 久久久久免费精品国产小说| 91亚洲性爱在线视频| 免费观看的毛片手机视频| 美女视频黄a视频全免费网站一区| 亚洲人成网站在线观看青青| a级毛片免费高清毛片视频| 亚洲欧洲校园自拍都市| 午夜一级免费视频| 亚欧乱色国产精品免费视频| 婷婷亚洲综合五月天小说| 久久久久国色AV免费观看性色| 色屁屁www影院免费观看视频| 亚洲乱码国产一区三区| 日韩在线免费视频| 边摸边吃奶边做爽免费视频网站 | 亚洲av永久中文无码精品| 亚洲欧洲久久av| 51精品视频免费国产专区| 青草久久精品亚洲综合专区| 亚洲精品无码高潮喷水在线| 99在线精品视频观看免费| 青青草97国产精品免费观看| 78成人精品电影在线播放日韩精品电影一区亚洲 |