我也是菜鸟,我喜欢这样写,两个FOR循环外面的计算冒泡次数里面的进行比较

解决方案 »

  1.   

    {code
    import java.util.*; 
     public class bubblesort
    {
    public static int[] a=new int[10];
     
     public static void bubblesort(int num)
    {      int temp;
     for(int i=num;i>1;i--)
     for(int j=0;j<num-1;j++)
       if(a[j]>a[j+1])
        {    temp=a[j];
             a[j]=a[j+1];
         a[j+1]=temp;
     
     }

     
        public static void main(String args[])
    {

    Scanner input=new Scanner(System.in);
    for(int index=0;index<10;index++)
    {
     
     
    a[index]=Integer.parseInt(input.next());

    }
    input.close();
    bubblesort.bubblesort(10);
    for(int index=0;index<10;index++)
    System.out.println(a[index]);
    }
    };
    code}
      

  2.   

    楼上的,没有编辑好,你把开头的{code和结尾的code}去掉试试看
    我也是刚学的,代码写的有些乱请见谅啊
      

  3.   

    for(int i=num;i>1;i--) 
     for(int j=0;j <num-1;j++) 
       if(a[j]>a[j+1]) 
        {    temp=a[j]; 
             a[j]=a[j+1]; 
         a[j+1]=temp; 
      
     } 
    }  
    你把冒泡的算法看好了,
        自己跟到程序走一边,
          在for 里,
       搞的几次,就晓的了呀
      

  4.   

    恩..
      thank you all of person!!
      

  5.   


    就下面这样
    int temp=0;
    for(int i=number;i>1;i--)  
     for(int j=0;j  <number-1;j++)  
       if(a[j]>a[j+1]){
             temp=a[j];  
             a[j]=a[j+1];  
             a[j+1]=temp;  
       
     }  
    }  
      

  6.   

       1.  /** 
       2.  * 冒泡排序算法 
       3.  */  
       4. public class BubbleSort {  
       5.     public static void sort(int[] values) {  
       6.         int temp;  
       7.         for (int i = 0; i < values.length; ++i) {  
       8.             for (int j = 0; j < values.length - i - 1; ++j) {  
       9.                 if (values[j] > values[j + 1]) {  
      10.                     temp = values[j];  
      11.                     values[j] = values[j + 1];  
      12.                     values[j + 1] = temp;  
      13.                 }  
      14.             }  
      15.         }  
      16.     }  
      17. }