快速排序算法 java语言描述

来源:互联网 发布:手机象棋软件排名 编辑:程序博客网 时间:2024/05/21 10:52
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);}}


原创粉丝点击