package com.cn.sotred; import java.util.Arrays; public class BubbleSort { public static void main(String[] args) { int[] a = { 50, 33, 26, 1, 7, 9, 46, 2, 23, 55, 72, 234, 34, 6, 21, 74, 99, 65, 3, 5, 675, 23 }; System.out.println(Arrays.toString(bubbleSotr(a))); ; } //将数组竖着放置,小的下标在上,大的下标在下 private static int[] bubbleSotr(int[] a){ int temp; for (int i = 0; i < a.length-1; i++) {//控制循环次数,同时也指明本遍扫描终点下标,即有序段和无序段分界的下标 for (int j = a.length-2; j >= i; j--) {//从倒数第二个和倒数第一个开始比较; j >= i用来判断是否已经到了本遍扫描终点 if (a[j]>a[j+1]) { temp = a[j+1]; a[j+1] = a[j]; a[j] = temp; } } } return a; } }