JAVA排序算法--起泡排序

若想实现数组中较大的数先沉下去 而不是小的是飘上来  只需把外层循环i的初值赋值为a.length-1 ; i>0 ; i-- 即可

/**  
 * JAVA排序算法--起泡排序  
 *   
 * @author 千醉  JAVA新手菜鸟群 32413139  
 *   
 */     
public class BubbleSort {   
  public static void main(String[] args) {   
    int[] a = { 10, 32, 1, 9, 5, 7, 12, 0, 4, 3 };   
  
    System.out.print("排序前: ");   
  
    for (int i = 0; i < a.length; i++)   
      System.out.printf("%3s", a[i]);   
  
    System.out.println();   
   
    BubbleSort.bubbleSort(a);   
  
    System.out.print("排序后: ");   
  
    for (int i = 0; i < a.length; i++)   
      System.out.printf("%3s", a[i]);   
  
    System.out.println();   
  }   
  
  public static void bubbleSort(int[] a) {   
    boolean change = false;   
    int temp;   
    int count = 0;   
         
	for (int i = 0; i<a.length; i++)
	{
		for(int j=i+1 ; j<a.length ;j++)
		{
			if(a[i]>a[j])
			{
				temp = a[i] ;
				a[i] = a[j] ;
				a[j] = temp ;
				count++ ;
				change = true ;
			}
		}
        if (change) 
        {   
	        System.out.print("第" + count + "趟交换:  ");   
	        for (int k = 0; k < a.length; k++)   
	          System.out.print(a[k] + " ");   
	  
	        System.out.println();   
        }   
    }   
  }   
}   


你可能感兴趣的:(java,算法,J#)