1 Public classDemo {2 3 Public Static voidMain (string[] args) {4 int[] arr =New int[10];5 for(inti = 0; i < arr.length; i++) {6Random rd =NewRandom ();7Arr[i] = rd.nextint (100);8 }9SYSTEM.OUT.PRINTLN ("array-to-order"));Ten for(intj = 0; J < Arr.length; J + +) { OneSystem.out.print (Arr[j] + "\ T"); A } - System.out.println (); - System.out.println (); the - //Select Sort - for(inti = 1; i < arr.length; i++) { - intMax = 0; + for(intj = 0; J <= Arr.length-i; J + +) { - if(arr[j]>Arr[max]) { +Max =J; A } at } - inttemp = arr[arr.length-i]; -Arr[arr.length-i] =Arr[max]; -Arr[max] =temp; - } -System.out.println ("Select sort Result--"); in for(intj = 0; J < Arr.length; J + +) { -System.out.print (Arr[j] + "\ T"); to } + System.out.println (); - the //Bubble Sort * for(inti = 1; i < arr.length; i++) { $ for(intj = 0; J < Arr.length-1; J + +) {Panax Notoginseng if(arr[j+1]<Arr[j]) { - inttemp = arr[j+1]; theARR[J+1] =Arr[j]; +ARR[J] =temp; A } the } + } -System.out.println ("bubble sort result--"); $ for(intj = 0; J < Arr.length; J + +) { $System.out.print (Arr[j] + "\ T"); - } - System.out.println (); the - //Quick SortWuyiQuickSort (arr, 0, Arr.length-1); theSystem.out.println ("Quick Sort results--"); - for(intj = 0; J < Arr.length; J + +) { WuSystem.out.print (Arr[j] + "\ T"); - } About System.out.println (); $ - //Direct Insert Sort - for(inti = 1; i < arr.length; i++) { - inttemp =Arr[i]; A intJ; + for(j = i-1; J >= 0 && arr[j] > temp; j--) { theArr[j + 1] =Arr[j]; - } $Arr[j + 1] =temp; the } theSYSTEM.OUT.PRINTLN ("Direct insert sort result--"); the for(intj = 0; J < Arr.length; J + +) { theSystem.out.print (Arr[j] + "\ T"); - } in System.out.println (); the the //Sort by using sort () About Arrays.sort (arr); theSystem.out.println ("Sort results with sort ()"); the for(intj = 0; J < Arr.length; J + +) { theSystem.out.print (Arr[j] + "\ T"); + } - System.out.println (); the Bayi } the the Public Static voidQuickSort (int[] A,intLintr) { - - if(L <r) { the inti,j,x; the thei =l; thej =R; -x =A[i]; the while(I <j) { the while(I < J && A[j] >x) { thej--;//find the first number less than x from right to left94 } the if(I <j) { thea[i++] =A[j]; the }98 while(I < J && A[i] <x) { Abouti++;//find the first number greater than x from left to right - }101 if(I <j) {102a[j--] =A[i];103 }104 } theA[i] =x;106QuickSort (A, L, i-1);/*Recursive invocation*/107QuickSort (A, i+1, R);/*Recursive invocation*/108 }109 } the 111}
View CodeSort Results
To sort an array-->17 0, and then Select the sort result -->0 The results of the bubble sort result -->0 The results of the Rapid sequencing of -->0 The results of the Direct insertion of-->0 38 The results of the sort () -->0 38 84
View Code
Select, bubble, insert, direct sort algorithm