Packagecom.xxx.xxx;/*** 1. Direct Select Sort * 2. Reverse sort *@authorAdministrator **/ Public classdemo11 {//Direct Select Sort /*** Direct selection sort is faster than bubble sort * Basic idea: Compare the specified sort position with other array elements to meet on the exchange * (Note: Here the bubble sort is not exchanged, instead of exchanging adjacent elements, instead of exchanging the element that satisfies the condition with the specified sort position) * With the bubbling row The direct selection of the order is much less than the number of exchanges*/ Public Static voidMain (string[] args) {//Direct Select Sort intindex; int[] array = {63,4,24,1,3,15};//creating an array is a random order for(inti=1;i<=array.length;i++) {//start at 1, judging is less than or equal toindex = 0; for(intj = 1;j<= array.length-i;j++) {//start at 1, judging is less than or equal, length-1 if(Array[j] > Array[index]) {//If Judgmentindex =J; } } //swap two numbers on position array_sort.length-1 and Index_sort (maximum) inttemp = Array[array.length-i];//Save the first element in a temporary variableArray[array.length-i] = Array[index];//Save the second one in the firstArray[index] = temp;//Save the temporary variable (that is, the first one) in a second element } //Traverse Output for(intK:array) {System.out.print (">" +k); } System.out.println ("\ n--------------------------"); //2. Reverse Sort /*** Basic idea: Replace the last element of the array with the first element, the penultimate element replaces the second element, and so on, until all the array elements are reversed instead of the reverse sort is replaced by the elements on either side of the array, so only half of the loop's length is required * If the array length is 7, then the for loop only needs to loop 3 times*/ int[] A = {10,20,30,40,50,60}; //iterate output after array inversion for(intx:a) {System.out.print (x+ "\ T"); } System.out.println (); inttemp; intlen = a.length;//Get Array length for(intI=0;I<LEN/2; i++) {//according to the algorithm, length except 2temp = A[i];//ExchangeA[i] = a[len-1-i]; A[len-1-i] =temp; } //iterate output after array inversion for(intx:a) {System.out.print (x+ "\ T"); } }}
10. Direct select sort and reverse sort