都是自己写的,可能有不对的地方
1.冒泡排序
public static void maopao(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 b = a[j];
a[j] = a[j+1];
a[j+1] = b;
}
}
}
}
2.快速排序
public static void main(String[] args) throws Exception {
int[] aa = new int[]{9, 4, 6, 8, 23, 5, 78, 44, 13, 34};
kuaipai(aa, 0, 9);
}
public static void kuaipai(int[] array, int i, int j) {
if(i<j) {
int a = i;
int b = i + 1;
int c = j;
while (b < c) {
while (array[c] > array[a]) {
c--;
}
if(c >= a && c >= b) {
int d = array[a];
array[a] = array[c];
array[c] = d;
a = c;
System.out.println(Arrays.toString(array));
}
while (array[b] < array[a]) {
b++;
}
if (b < a && b < c) {
int d = array[a];
array[a] = array[b];
array[b] = d;
a = b;
System.out.println(Arrays.toString(array));
}
}
kuaipai(array, i, a-1);
kuaipai(array, a+1, j);
}
}
结果:
[5, 4, 6, 8, 23, 9, 78, 44, 13, 34]
[5, 4, 6, 8, 9, 23, 78, 44, 13, 34]
[5, 4, 6, 8, 9, 23, 78, 44, 13, 34]
[4, 5, 6, 8, 9, 23, 78, 44, 13, 34]
[4, 5, 6, 8, 9, 13, 78, 44, 23, 34]
[4, 5, 6, 8, 9, 13, 23, 44, 78, 34]
[4, 5, 6, 8, 9, 13, 23, 44, 78, 34]
[4, 5, 6, 8, 9, 13, 23, 34, 78, 44]
[4, 5, 6, 8, 9, 13, 23, 34, 44, 78]
[4, 5, 6, 8, 9, 13, 23, 34, 44, 78]