posts - 14, comments - 15, trackbacks - 0, articles - 0
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          java 排序算法四例

          Posted on 2009-06-11 11:10 析木 閱讀(245) 評論(0)  編輯  收藏 所屬分類: JAVA
          class weijySort {
            
          3    public static void main (String[] args) {
            
          4        int gohome[] = new int[]{12,7,54,21,1,4,65,76,34,9,3,6};    
            
          5        System.out.println("插入排序算法");
            
          6//        InsertionSort(gohome);
            7        System.out.println("-------------------------------------------");
            
          8        System.out.println("選擇排序算法");
            
          9//        SelectSort(gohome);
           10        System.out.println("-------------------------------------------");
           
          11        System.out.println("冒泡排序算法");
           
          12//        BubbleSort(gohome);
           13        System.out.println("-------------------------------------------");
           
          14        gohome =QuickSort(gohome);
           
          15        
           
          16        for (int t=0; t<gohome.length;t++)
           
          17        {
           
          18            System.out.print(gohome[t]+"--");
           
          19        }
           
           
          20    }

           
          21    
           
          22    //插入排序算法
           23    public static void InsertionSort(int[] goal)
           
          24    {    
           
          25        for (int i = 1; i<goal.length; i++)
           
          26        {    int now = i;
           
          27            int frank = goal[i];            
           
          28            while (now>0 && goal[now-1<= frank)
           
          29            {
           
          30                goal[now]=goal[now-1];
           
          31                now--;            
           
          32            }

           
          33            goal[now]=frank;
           
          34            
           
          35        
           
          36        }
              
           
          37        
           
          38            
           
          39        for (int t=0; t<goal.length;t++)
           
          40        {
           
          41            System.out.print(goal[t]+"--");
           
          42        }

           
          43    }

           
          44    
           
          45    //選擇排序算法    
           46    public static void SelectSort(int[] goal)
           
          47    {        
           
          48        int max;
           
          49        int stmp;    
           
          50        for (int i = 0; i<goal.length-1; i++)
           
          51        {
           
          52            max=i;
           
          53            for (int j = i+1; j<goal.length; j++)
           
          54                if(goal[j]>goal[max])
           
          55                    max=j;
           
          56                                    
           
          57            stmp = goal[i];
           
          58            goal[i]=goal[max];
           
          59            goal[max]=stmp;            
           
          60        
           
          61        }
              
           
          62        for (int t=0; t<goal.length;t++)
           
          63        {
           
          64            System.out.print(goal[t]+"--");
           
          65        }

           
          66            
           
          67    
           
          68    }

           
          69    
           
          70    //冒泡排序算法    
           71    public static void BubbleSort(int[] goal)
           
          72    {    int stmp;
           
          73        for (int i = 1; i< goal.length; i++)
           
          74        {
           
          75            for(int j=0; j<i;j++)
           
          76            {
           
          77                if(goal[i]>goal[j])
           
          78                {
           
          79                    stmp=goal[i];
           
          80                    goal[i]=goal[j];
           
          81                    goal[j]=stmp;    
           
          82                }
              
           
          83            }
              
           
          84    
           
          85        }

           
          86        for (int t=0; t<goal.length;t++)
           
          87        {
           
          88            System.out.print(goal[t]+"--");
           
          89        }

           
          90    }

           
          91    
           
          92    //快速排序算法
           93    public static int[] QuickSort(int[] number) {
           
          94      QuickSort(number, 0, number.length-1);
           
          95      return number ;
           
          96      }

           
          97    private static void QuickSort(int[] number,int left, int right) {
           
          98        int stmp;
           
          99      if(left < right) {
          100          System.out.println(left+" | "+right+" | "+(left+right)/2);
          101          int s = number[(left+right)/2];
          102          int i = left - 1;
          103          int j = right + 1;
          104          while(true{
          105                // 向右找
          106                while(number[++i] > s) ;
          107                  // 向左找
          108                while(number[--j] < s) ;
          109                  if(i >= j)
          110                      break;             
          111                stmp = number[i];
          112                  number[i] = number[j];
          113                  number[j] = stmp;              
          114              }

          115          QuickSort(number, left, i-1); // 對左邊進行遞回
          116          QuickSort(number, j+1, right); // 對右邊進行遞回
          117      }
               
          118      }

          119 }

          我要啦免费统计
          主站蜘蛛池模板: 刚察县| 凤冈县| 怀柔区| 兴隆县| 祁门县| 嵩明县| 汉阴县| 成安县| 牙克石市| 木兰县| 天津市| 海晏县| 蕉岭县| 大冶市| 蕲春县| 公主岭市| 邯郸市| 临朐县| 阿拉善右旗| 北宁市| 青海省| 鹤峰县| 抚宁县| 莱芜市| 奎屯市| 兴城市| 蓬安县| 堆龙德庆县| 都安| 巴林右旗| 池州市| 河源市| 达州市| 湘西| 白银市| 满城县| 东乌珠穆沁旗| 黄梅县| 连云港市| 林甸县| 高平市|