Java基础之冒泡法的实现


Java基础之冒泡法的实现,没有注释,不过应该还好理解。

啥也不多说了,直接上代码。

  1. /** 
  2.  *  冒泡法排序 
  3.  */  
  4. public class MaoPao {  
  5.       
  6.     public static void main(String[] args) {  
  7.         maopao();  
  8.     }  
  9.       
  10.     public static void maopao(){  
  11.         int[] x = {12,22,34,14,1,9,2};  
  12.         for(int m =0;m<x.length;m++){  
  13.             System.out.print(x[m]+", ");  
  14.         }  
  15.         System.out.println();  
  16.           
  17.         for(int i = 0;i < x.length; i++){  
  18.             for(int j = 0;j < x.length-i-1; j++){  
  19.                 if(x[j] > x[j+1]){  
  20.                     int temp = x[j];  
  21.                     x[j] = x[j+1];  
  22.                     x[j+1] = temp;  
  23.                 }  
  24.             }  
  25.         }  
  26.           
  27.         for(int k = 0;k < x.length; k++){  
  28.             System.out.print(x[k]+", ");  
  29.         }  
  30.     }  
  31.   
  32. }  

相关内容