冒泡排序

public class ArrayUtil {

  public static void printArray(int[] array) {
    System.out.print("{");

    for (int i = 0; i < array.length; i++) {
      System.out.print(array[i]);
      if (i < array.length - 1) {
        System.out.print(",");
      }
    }

    System.out.println("}");
  }

}

 

public class BubbleSort {

/**
* 冒泡排序
*
* @param array
*/

  public static void bubbleSort(int[] array) {

    if (array == null || array.length < 2) {
      return;
    }

    int temp;
    for (int i = 0; i < array.length; i++) {
      for (int j = i + 1; j < array.length; j++) {
        if (array[i] > array[j]) {
          temp = array[i];
          array[i] = array[j];
          array[j] = temp;
        }
      }
    }

  }

  public static void main(String[] args) {
  // TODO Auto-generated method stub
    int[] a = { 38, 65, 97, 76, 13, 27, 49, 78, 34, 12, 64, 1 };
    ArrayUtil.printArray(a);
    bubbleSort(a);
    ArrayUtil.printArray(a);
  }

}

posted @ 2015-07-31 15:40  studyForAndroid  阅读(176)  评论(0)    收藏  举报