/** * * @authorAdministrator * Function: Fast sorting of exchange sort*/ PackageCom.test1;ImportJava.util.Calendar; Public classQuickSort { Public Static voidMain (string[] args) {//TODO auto-generated Method Stub//int[] arr = {1, 6, 0,-1, 9, -100, -90}; int[] arr =New int[10000]; for(inti = 0; i < arr.length; i++) { //let the program randomly produce a 1-10000 number//Math.random will produce a number from 0 to 1.Arr[i] = (int) (Math.random () *10000); } Quick Quick=NewQuick (); //Print the system time before sorting//Calendar is a single open mode, there is only one in the system, and only need an instance, not new out of the//Java 23 in total modeCalendar cal =calendar.getinstance (); System.out.println ("Before sorting:" +cal.gettime ()); Quick.sort (0, Arr.length-1, arr); //Print the system time after sorting//because it is single-open, all the first to get the instance backCal =calendar.getinstance (); System.out.println ("After sorting:" +cal.gettime ());//System.out.println ("The result after sorting is:");//for (int i = 0; i < arr.length; i++) {//System.out.print (arr[i]+ "");// } }}//Quick Sort Methodclassquick{ Public voidSortintLeftintRightint[] Array) { intL =Left ; intR=Right ; intPivot = array[(left+right)/2]; inttemp = 0; while(L <r) { while(Array[l] < pivot) l++; while(Array[r] > Pivot) r--; if(L >=R) Break; Temp=Array[l]; ARRAY[L]=Array[r]; ARRAY[R]=temp; } //System.out.println ("The current array is");//for (int i = 0; i< 5; i++)// {//System.out.println ("+array[i");// } if(L = =r) {L++; R--; } if(Left <r) sort (Left,r,array); if(right>l) sort (l,right,array); } }
Fast Sorting Method Quicksort