# 测试样例：

[1,2,3,5,2,3],6 [1,2,2,3,3,5] 我的提交

# -- coding:utf-8 --

class BubbleSort: def bubbleSort(self, A, n): # write code here k = n - 1 for _ in range(n - 1): for j in range(k): if A[j] > A[j+1]: A[j],A[j+1] = A[j+1],A[j] k = k - 1 return A 其他人能通过的 python 版本 （有现成的函数能用就用 ？。。。。）

# -- coding:utf-8 --

class BubbleSort: def bubbleSort(self, A, n): # write code here return sorted(A) 参考答案 public class BubbleSort {

``````public static void bubbleSort(int[] arr) {
if (arr == null || arr.length < 2) {
return;
}
for (int i = arr.length - 1; i >= 0; i--) {
for (int j = 0; j < i; j++) {
if (arr[j] > arr[j + 1]) {
swap(arr, j, j + 1);
}
}
}
}

public static void swap(int[] arr, int index1, int index2) {
int tmp = arr[index1];
arr[index1] = arr[index2];
arr[index2] = tmp;
}

public static int[] generateArray(int len, int range) {
if (len < 1) {
return null;
}
int[] arr = new int[len];
for (int i = 0; i < len; i++) {
arr[i] = (int) (Math.random() * range);
}
return arr;
}

public static void printArray(int[] arr) {
if (arr == null || arr.length == 0) {
return;
}
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}

public static boolean isSorted(int[] arr) {
if (arr == null || arr.length < 2) {
return true;
}
for (int i = 1; i < arr.length; i++) {
if (arr[i - 1] > arr[i]) {
return false;
}
}
return true;
}

public static void main(String[] args) {
int len = 10;
int range = 10;
int testTimes = 50000;
for (int i = 0; i < testTimes; i++) {
int[] arr = generateArray(len, range);
bubbleSort(arr);
if (!isSorted(arr)) {
System.out.println("Wrong Case:");
printArray(arr);
break;
}
}

int len2 = 13;
int range2 = 10;
int testTimes2 = 50000;
for (int i = 0; i < testTimes2; i++) {
int[] arr = generateArray(len2, range2);
bubbleSort(arr);
if (!isSorted(arr)) {
System.out.println("Wrong Case:");
printArray(arr);
break;
}
}
}
``````

}