package 备份类;
public class 快速排序 {
//主类测试
public static void main(String[] args) {
int[] aa = { 6, 5, 4, 3, 3, 67, 6, 89, 434, 2, 4 };
outArr(aa);
quickSort(aa);
outArr(aa);
}
//数组输出
public static void outArr(int[] a) {
int n = a.length;
for (int i = 0; i < n; i++) {
System.out.print(a[i] + " ");
}
System.out.println();
}
//分割
public static int division(int[] a, int low, int heigh) {
int base = a[low];
while (low < heigh) {
while (low < heigh && a[heigh] > base) {
heigh--;
}
if (low < heigh)
a[low++] = a[heigh];// 或者不要++
while (low < heigh && a[low] < base) {
low++;
}
if (low < heigh)
a[heigh--] = a[low];// 或者不要--
}
a[low] = base;
return low;
}
//排序
public static void preQuickSort(int[] a, int low, int heigh) {
if (low < heigh) {
int i = division(a, low, heigh);
preQuickSort(a, low, i - 1);
preQuickSort(a, i + 1, heigh);
}
}
//快速排序
public static void quickSort(int[] a) {
if (a.length > 0) {
preQuickSort(a, 0, a.length - 1);
}
}
}
JAVA排序算法---快速排序
最新推荐文章于 2023-06-04 10:54:50 发布