快排时间复杂度:nlogn
java代码1:对输入数组实现快排
import java.util.Scanner; public class Temp { public static void main(String[] args){ Scanner s = new Scanner(System.in); int[] arr = new int[10]; for(int i=0;i<arr.length;i++){ arr[i] = s.nextInt(); } printArr(arr); Temp t = new Temp(); t.quickSort(arr, 0, arr.length-1); printArr(arr); } public void quickSort(int[] a,int left,int right){ int i = left; if(left<right){ for(int j = left;j<a.length;j++){ if(a[j]<a[right]){ swap(a,i,j); i = i+1; } } swap(a,i,right); quickSort(a,left,i-1); quickSort(a,i+1,right); } } private void swap(int[] a,int i,int j){ int temp; temp = a[i]; a[i] = a[j]; a[j] = temp; } public static void printArr(int[] a){ for(int i=0;i<a.length;i++){ System.out.print(a[i]+" "); } System.out.println(); } }
输出结果:
java代码2:对随机产生的10组包含一百万个数字(大小在0-1000之间)的数组进行快排和随机快排,并比较其排序时间。之后再对有序数组分别进行快排和随机快排,比较其性能。
package quickSort; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Random; public class QuickSort { final static int arrlength = 1000000; static int arr[] = new int[arrlength]; static int a1[],a2[]; static SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); static long between1; static long between2; public static void main(String[] args) throws ParseException{ int num=0; QuickSort q = new QuickSort(); QuickSort s= new QuickSort(); for (int loop = 0;loop<10;loop++){ int count = 0; Random random = new Random(); while(count<arrlength){ arr[count] = random.nextInt(1000); count++; } a1=arr.clone(); a2=arr.clone(); // q.printArr(arr); java.util.Date begintime = dfs.parse(dfs.format(new Date())); q.quick(a1,0,a1.length-1); java.util.Date middletime = dfs.parse(dfs.format(new Date())); s.random(a2,0,a2.length-1); java.util.Date endtime = dfs.parse(dfs.format(new Date())); between1=middletime.getTime()-begintime.getTime(); between2=endtime.getTime()-middletime.getTime(); System.out.print("快速排序耗时:"+between1+"ms "); // System.out.println(); // q.printArr(a1); System.out.println("随机快排耗时:"+between2+"ms "); // q.printArr(a2); if(between1>between2) num++; } System.out.println("随机快排更快的次数:"+num); java.util.Date begintime = dfs.parse(dfs.format(new Date())); q.quick(a2,0,a1.length-1); java.util.Date middletime = dfs.parse(dfs.format(new Date())); s.random(a2,0,a2.length-1); java.util.Date endtime = dfs.parse(dfs.format(new Date())); between1=middletime.getTime()-begintime.getTime(); between2=endtime.getTime()-middletime.getTime(); System.out.println("对有序序列排序: "); System.out.println("快速排序耗时:"+between1+"ms "); // q.printArr(a2); System.out.println("随机快排耗时:"+between2+"ms "); // s.printArr(a2); } public void quick(int[] a,int left,int right){ if (left>=right){ return; } int i = left; for(int j = left; j < right;j++){ if(a[j]<a[right]){ swap(a,i,j); i++; } } swap(a,i,right); // printArr(a); quick(a,left,i-1); quick(a,i+1,right); } private void swap(int[] a,int i,int j){ int temp=a[i]; a[i]=a[j]; a[j]=temp; } public void random(int[] a,int left,int right){ if(left >= right){ return; } Random r = new Random(); int index = (r.nextInt(right-left))+left; swap(a,index,right); quick(a,left,right); } private void printArr(int[] a){ for(int i=0;i<a.length;i++){ System.out.print(a[i] + " "); } System.out.println(); } }
输出结果:
一百万的数据量任然太小,随机快排的效果并不明显(效率并不是确定的),但对于有序数组,随机快排明显要更快。