1、数组的冒泡排序
public void bubbleSort(int a[]) {
int n = a.length;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - 1; j++) {
if (a[j] > a[j + 1]) {
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
}
2.数组的选择排序
public void selectSort(int a[]) {
for (int n = a.length; n > 1; n--) {
int i = max(a, n);
int temp = a[i];
a[i] = a[n - 1];
a[n - 1] = temp;
}
}
3.数组的插入排序
public void insertSort(int a[]) {
int n = a.length;
for (int i = 1; i < n; i++) { //将a[i]插入a[0:i-1]
int t = a[i];
int j;
for (j = i - 1; j >= 0 && t < a[j]; j--) {
a[j + 1] = a[j];
}
a[j + 1] = t;
}
}
4.利用Arrays带有的排序方法快速排序
import java.util.Arrays; 2 public class Test2{
public static void main(String[] args){
int[] a={5,4,2,4,9,1};
Arrays.sort(a); //进行排序
for(int i: a){
System.out.print(i);
}
}
}