package test;
import java.util.Random;
public class Test1 {
static int num = 0;
//
// 快速排序算法
public void quickSort(int[] a, int head, int tail) {
if (head < tail) {
int low = head;
int height = tail;
int temp = a[low];
while (low < height) {
while (low < height && a[height] >= temp) {
--height;
}
a[low] = a[height];
while (low < height && a[low] <= temp) {
++low;
}
a[height] = a[low];
}
a[height] = temp;
quickSort(a,head,low-1);
quickSort(a,low+1,tail);
num++;
}
}
//
public static void main(String[] args) {
int[] a = new int[20];
for (int i = 0; i < 20; i++) {
a[i] = (int) (Math.random()*100);
}
for (int i:a) {
System.out.print(i+" ");
}
System.out.println();
new Test1().quickSort(a, 0, a.length-1);
for (int i:a) {
System.out.print(i+" ");
}
System.out.println("\n"+new Test1().num);
}
}
快速排序算法 java语言描述
最新推荐文章于 2020-07-18 16:17:47 发布