(用来做笔记,欢迎交流讨论!)
贴上代码:
package com.fei.example.lib;
public class BubbleSort {
public void bubbleSort(int[] A, int n) {
int temp;
for (int i = 1; i <= n - 1; i++) {
for (int j = 0; j <= n - i - 1; j++) {
if (A[j] > A[j + 1]) {
temp = A[j];
A[j] = A[j + 1];
A[j + 1] = temp;
}
}
}
}
//测试数据
public static void main(String[] args) {
BubbleSort a = new BubbleSort();
int[] array = {49, 38, 65, 97, 76, 13, 27, 49, 55, 04};
int n = array.length;
a.bubbleSort(array, n);
for (int i = 0; i <= n - 1; i++) {
System.out.print(array[i] + " ");
}
}
}