集册 Java实例教程 Java气泡排序

Java气泡排序

欢马劈雪     最近更新时间:2020-01-02 10:19:05

504
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
Java气泡排序

 

public class Main {/*nowjava.com 提 供*/


  public static void main(String[] args) {

    int intArray[] = new int[] { 5, 9, 3, 4, 1, 8 };

    System.out.println("Array Before Bubble Sort");

    for (int i = 0; i < intArray.length; i++) {

      System.out.print(intArray[i] + " ");

    }


    bubbleSort(intArray);


    System.out.println("");


    System.out.println("Array After Bubble Sort");

    for (int i = 0; i < intArray.length; i++) {

      System.out.print(intArray[i] + " ");

    }

  }
/** 时代Java 提 供 **/

  private static void bubbleSort(int[] intArray) {

    int n = intArray.length;

    int temp = 0;

    for (int i = 0; i < n; i++) {

      for (int j = 1; j < (n - i); j++) {

        
展开阅读全文