冒泡排序
2017-04-04 本文已影响0人
_Raye
package org.mobiletrain;
//冒泡排序
public class Test07 {
public static void main(String[] args) {
int[] x = {23, 67, 12, 99, 58, 77, 88, 4, 45, 81};
bubblesort(x);
for (int a: x) {
System.out.print(a + " ");
}
}
private static void bubblesort(int[] array) {
boolean swapped = true;
for(int i = 1; swapped && i < array.length; i++){
swapped = false;
for(int j = 0; j < array.length - i; j++){
if (array[j] > array[j + 1]) {
//交换两个元素
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
swapped = true;
}
}
}
}
}