public static void main(String[] args) {
// TODO Auto-generated method stub
int[] arr = { 37, 47, 23, 100, 19, 56, 56, 99, 9 };
Test test = new Test();
//test.bubbleSort(arr);
test.selectSort(arr);
System.out.println(Arrays.toString(arr));
}
// 冒泡排序:相邻元素两两比较,比较完一趟,最值出现在末位
public void bubbleSort(int array[]) {
for (int i = 0; i < array.length - 1; i++) {//比较趟数
for (int j = 0; j < array.length - 1 - i; j++) {//比较次数
if (array[j] > array[j + 1]) {
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
}
// 选择排序:在数组中找到最值和第一个比较
public void selectSort(int array[]) {
for (int i = 0; i < array.length - 1; i++){
for (int j = i + 1; j < array.length; j++)
if (array[i] > array[j]) {
int temp= array[i];
array[i] = array[j];
array[j] = temp;
}
}
}