快速排序演算法 java語言描述
阿新 • • 發佈:2019-02-06
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); } }