• Index

冒泡排序

Last updated: ... / Reads: 131 Edit

代码

package sort;

/**
 * 冒泡排序
 *
 * 注:leetcode提交不了,因为算法速度太慢。
 *
 * @author javaself
 */
public class BubbleSort {
  public int[] bubbleSort(int a[]){
    int n = a.length;

    //双循环
    for (int i = 0; i < n; i++) {
      for (int j = i+1; j < n; j++) {
        //比较数据
        if (a[i] > a[j]) { //如果左边数据大,那么交换数据
          int temp = a[i];
          a[i] = a[j];
          a[j] = temp;
        }
      }
    }

    return a;
  }
}


Comments

Make a comment

  • Index