冒泡排序

public class BubbleSort {
public static void main(String[] args) {
// int arr[] = {3, 9, -1, 10, -2};
int[] arr = new int[10000];
for(int i=0;i arr[i]=(int)(Math.random()*10000000);
}
bubbleSort(arr);
System.out.println(Arrays.toString(arr));
}

public static void bubbleSort(int arr[]) {
int temp = 0;
boolean flag = false;
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
flag = true;
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
if (!flag) {
break;
} else {
flag = false;
}
}

}
}

你可能感兴趣的:(冒泡排序)