常見排序法

          Posted on 2006-11-10 23:03 黑夜ちつ獨行者 閱讀(421) 評論(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);
          }
          }

          ?


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


          網站導航:
           
          主站蜘蛛池模板: 沙雅县| 十堰市| 临潭县| 高阳县| 海兴县| 新兴县| 应用必备| 女性| 邵武市| 辉县市| 同仁县| 元氏县| 侯马市| 绥棱县| 临邑县| 离岛区| 长治县| 甘德县| 长寿区| 密云县| 浪卡子县| 那坡县| 潍坊市| 土默特右旗| 南昌县| 澎湖县| 惠水县| 常熟市| 五家渠市| 怀集县| 手游| 阜阳市| 庆阳市| 桦川县| 高清| 文水县| 麻城市| 岗巴县| 时尚| 榆树市| 绥滨县|