冒泡排序JAVA实现


冒泡排序JAVA实现

public class MaoPao {

/**
* @冒泡排序
*/

static int arry[];

//交换元素方法
public static void huan(int one,int two){
   int temp=arry[one];
   arry[one]=arry[two];
   arry[two]=temp;
 
}

//显示输出数组中的数据
public static void output(){
   for(int i=0;i<arry.length;i++){
    System.out.print(arry[i]+" ");
   }
}

//核心排序方法
public static void paixu(){
   for(int a=arry.length-1;a>1;a--){
    for(int b=0;b<a;b++){
     if(arry[b]>arry[b+1]){
      huan(b, b+1);
     }
    }
  
   }
 
}

public static void main(String[] args) {
   arry=new int[]{1,5,3,9,23,14,12,35};
   System.out.print("-----原始数据:----:");
   output();
   paixu();
   System.out.println(" ");
   System.out.print("-----排序后数据:----:");
   output();
 

}

}

相关内容