排序算法(冒泡排序)

冒泡的排序是两两比较(相邻的元素),以下算法算不上冒泡排序

//a需要排序的数组,n数组长度

public void bubbleSort(int a[],int n){

  int temp;

  for(int $i=0;$i<n-1;$i++)

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

      if(a[$i]>a[j]){

        temp=a[$i];

        a[$i]=a[j];

        a[j]=temp;

    }

  }

}

>>>正真的冒泡排序

public void bubbleSort(int a[],int n){

  int temp;

  for(int $i=0;$i<n-1;$i++)

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

      if(a[j-1]>a[j]){

        temp=a[j-1];

        a[j-1]=a[j];

        a[j]=temp;

    }

  }

}

原文地址:https://www.cnblogs.com/LvLoveYuForever/p/5743827.html