java冒泡排序

  1. public class Maopao {  
  2.     public static void main(String[] args) {  
  3.         int[] array={1,2,5,3,10,21,0};  
  4.         getSortD_X(array);  
  5.         getSortX_D(array);  
  6.     }  
  7.     public static void getSortD_X(int[] array){  
  8.         for (int i = 0; i array.length; i++) {  
  9.             for(int j=0;j<array.length-1;j++){  
  10.                 if(array[i]>array[j]){  
  11.                     int temp=array[i];  
  12.                     array[i]=array[j];  
  13.                     array[j]=temp;  
  14.                 }  
  15.             }  
  16.         }  
  17.         System.out.print("从大到小:");  
  18.         for (int i = 0; i array.length; i++) {  
  19.             if(i==array.length-1){  
  20.                 System.out.println(array[i]);  
  21.             }else{  
  22.                 System.out.print(array[i]+",");  
  23.             }  
  24.         }  
  25.     }  
  26.     public static void getSortX_D(int[] array){  
  27.         for (int i = 0; i <array.length; i++) {  
  28.             for(int j=0;j<array.length-i-1;j++){  
  29.                 if(array[j]>array[j+1]){  
  30.                     int temp=array[j];  
  31.                     array[j]=array[j+1];  
  32.                     array[j+1]=temp;  
  33.                 }  
  34.             }  
  35.         }  
  36.         System.out.print("从小到大:");  
  37.         for (int i = 0; i array.length; i++) {  
  38.             if(i==array.length-1){  
  39.                 System.out.println(array[i]);  
  40.             }else{  
  41.                 System.out.print(array[i]+",");  
  42.             }  
  43.         }  
  44.     }  
  45. }
原文地址:https://www.cnblogs.com/Yxxxxx/p/6858671.html