05_2_冒泡与选择排序算法
1. 例子
package Test;
public class NumSort {
public static void main(String[] args) {
int[] a = {5, 4, 712312, 1, 1119, 2, 1118};
print(a);
//selectSort(a);
bubbleSort(a);
print(a);
}
public static void print(int[] a) {
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
System.out.println("");
}
public static void selectSort(int[] a) {
int k, temp;
for (int i = 0; i < a.length; i++) {
k = i;
for (int j = k+1; j < a.length; j++) {
if (a[j] < a[k]) {
k = j;
}
}
if (k != i) {
temp = a[i];
a[i] = a[k];
a[k] = temp;
}
}
}
public static void bubbleSort(int[] a) {
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a.length-i-1; j++) {
int temp;
if(a[j+1] > a[j]) {
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
}
}