氣泡排序演算法
原理是臨近的數字兩兩進行比較,按照從小到大或者從大到小的順序進行交換,
這樣一趟過去後,最大或最小的數字被交換到了最後一位,
然後再從頭開始進行兩兩比較交換,直到倒數第二位時結束,其餘類似看例子
例子為從小到大排序,
代買僅供參考:
1 package com.zc.manythread; 2 /** 3 * 氣泡排序 4 * @author Administrator 5 * 6 */ 7 8 public class BSrot extends Sort{ 9 10 public void sort() { 11 try { 12 sort(date); 13 } catch (Exception e) { 14 // TODO: handle exception 15 e.printStackTrace(); 16 } 17 } 18 public int[] sort(int[] a)throws Exception{ 19 for (int i = a.length; --i>=0;) { 20 boolean swapped=false; 21 for (int j = 0; j < i; j++) { 22 if (a[j]>a[j+1]) { 23 int T=a[j]; 24 a[j]=a[j+1]; 25 a[j+1]=T; 26 swapped=true; 27 } 28 } 29 if (!swapped) { 30 return a; 31 } 32 } 33 return a; 34 } 35 public BSrot(int[] date) { 36 super(date); 37 // TODO Auto-generated constructor stub 38 } 39 40 }