package test;
import java.util.Arrays;
public class T1 {
public static void main(String[] args) {
int[] a = { 5, 5, 3, 9, 4, 0, 6, 1, 8, 7 };
sort1(a);
sort2(a);
sort3(a);
System.out.println(Arrays.toString(a));
}
// 冒泡排序
public static void sort1(int[] a) {
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a.length - 1 - i; j++) {
if (a[j] > a[j + 1]) {
int tmp = a[j + 1];
a[j + 1] = a[j];
a[j] = tmp;
}
}
}
}
// 选择排序
public static void sort2(int[] a) {
for (int i = 0; i < a.length; i++) {
for (int j = i + 1; j < a.length; j++) {
if (a[i] > a[j]) {
int tmp = a[j];
a[j] = a[i];
a[i] = tmp;
}
}
}
}
// 快速排序
public static void sort3(int[] a) {
quick(a, 0, a.length - 1);
}
public static void quick(int[] a, int i, int j) {
int s = i, e = j;
if (s >= e) {
return;
}
int m = a[i];
while (i < j) {
while (i < j && m < a[j]) {
j--;
}
a[i] = a[j];
while (i < j && m >= a[i]) {
i++;
}
a[j] = a[i];
}
a[i] = m;
quick(a, s, i - 1);
quick(a, i + 1, e);
}
}
快速排序
选择排序
冒泡排序