java冒泡排序代码 要能用的

解决方案 »

  1.   


      public static void sort(int[] data) {  
             int temp;  
             for (int i = 0; i < data.length; i++) {  
                 for (int j = data.length - 1; j > i; j--) {  
                     if (data[i] > data[j]) {  
                         temp = data[i];  
                         data[i] = data[j];  
                         data[j] = temp;  
                     }  
                 }  
             }  
         }  
       
         public static void main(String[] args) {  
             int[] a = { 4, 2, 3, 1, 5 };  
             sort(a);  
             for (int i = 0; i < a.length; i++)  
                 System.out.print(a[i] + " ");  
         }  
      

  2.   

    public class Sort {    public static void main(String[] args) {
            int[] i = { 1, 5, 6, 12, 4, 9, 3, 23, 39, 403, 596, 87 };
            System.out.println("冒泡排序的结果:");
            maoPao(i);
            System.out.println();
            System.out.println("选择排序的结果:");
            xuanZe(i);
            System.out.println();
            System.out.println("插入排序的结果:");
            chaRu(i);
            System.out.println();
            System.out.println("希尔排序的结果:");
            shell(i);
        }    // 冒泡排序算法
        public static void maoPao(int[] x) {
            for (int i = 0; i < x.length; i++) {
                for (int j = i + 1; j < x.length; j++) {
                    if (x[i] > x[j]) {
                        int temp = x[i];
                        x[i] = x[j];
                        x[j] = temp;
                    }
                }
            }
            for (int i : x) {
                System.out.print(i + " ");
            }
        }    // 选择排序算法
        public static void xuanZe(int[] x) {
            for (int i = 0; i < x.length; i++) {
                int lowerIndex = i;
                // 找出最小的一个索引
                for (int j = i + 1; j < x.length; j++) {
                    if (x[j] < x[lowerIndex]) {
                        lowerIndex = j;
                    }
                }
                // 交换
                int temp = x[i];
                x[i] = x[lowerIndex];
                x[lowerIndex] = temp;
            }
            for (int i : x) {
                System.out.print(i + " ");
            }
        }    // 插入排序算法
        public static void chaRu(int[] x) {
            for (int i = 1; i < x.length; i++) {// i从一开始,因为第一个数已经是排好序的啦
                for (int j = i; j > 0; j--) {
                    if (x[j] < x[j - 1]) {
                        int temp = x[j];
                        x[j] = x[j - 1];
                        x[j - 1] = temp;
                    }
                }
            }
            for (int i : x) {
                System.out.print(i + " ");
            }
        }    // 希尔排序算法
        public static void shell(int[] x) {
            // 分组
            for (int increment = x.length / 2; increment > 0; increment /= 2) {
                // 每个组内排序
                for (int i = increment; i < x.length; i++) {
                    int temp = x[i];
                    int j = 0;
                    for (j = i; j >= increment; j -= increment) {
                        if (temp < x[j - increment]) {
                            x[j] = x[j - increment];
                        } else {
                            break;
                        }
                    }
                    x[j] = temp;
                }
            }        for (int i : x) {
                System.out.print(i + " ");
            }
        }
    }
      

  3.   

    public class BubbleSort {    
      private Number []source;    
      public BubbleSort(Number []source) {    
        this.source=source;     
      }    
      public Number[] doSort() {     
        for(int i=source.length-1;i>1;i--) {    
          for(int j = 0; j < i; j++)    
            if(source[j].doubleValue() > source[j + 1].doubleValue()){    
              Number tmp=source[j];    
              source[j]=source[j+1];    
              source[j+1]=tmp;    
            }    
        }    
        return source;     
      }    
      public static void display(Number []source) {     
        for(int i=0;i<source.length;i++) {     
          System.out.println("source["+i+"] = "+source[i]);     
        }     
      }    
      public static void main(String[] args) {     
        Number []source={new Integer(4),new Double(2.56),new Float(9.11),    
          new Long(2),new Integer("2"),new Double(5.999999999)};     
        BubbleSort.display(source);     
        BubbleSort bubble=new BubbleSort(source);     
        source=bubble.doSort();     
        BubbleSort.display(source);     
      }     
    }