1 /**2 * Java Common sort3 * @authorlitaiqing4 *5 */6 Public classSorttest {7 8 /*9 * Direct Insert SortTen * Basic ideas: One * In the set of numbers to sort, assume that the number of front (n-1) [n>=2] is already in order, A * Now to insert the nth number into the ordinal number in front, so that the n number is also in good order. - * So repeat the cycle until all the order is sorted . - */ the Public Static voidInsertsort (int[] arr) { - for(inti = 1; i < arr.length; i++){ - intj = I-1; - for(; J > 0 && arr[i] < arr[j];j--){ +ARR[J+1] =Arr[j]; - } +ARR[J+1] =Arr[i]; A } at } - - /* - * Quick and easy sorting - * Basic ideas: - * In the group of numbers to be sorted, select the smallest number to exchange with the first position; in * Then in the rest of the number to find the smallest and second position of the number exchange, - * So loop to the second and last number comparison. to */ + Public Static voidSelectsort (int[] arr) { - for(inti = 0; i < arr.length-1; i++){ the for(intj = i + 1;j < arr.length; J + +){ * if(Arr[i] >Arr[j]) { $Arr[i] ^=Arr[j];Panax NotoginsengARR[J] ^=Arr[i]; -Arr[i] ^=Arr[j]; the } + } A } the } + - /* $ * Bubble Sort $ * Basic ideas: - * In the set of numbers to be sorted, the total number in the range that is not currently sequenced, - * top-down on the adjacent two number of comparisons and adjustments, so that the larger number to sink, smaller upward. the * That is, each time a comparison of two adjacent numbers is found to be reversed when sorting and sorting are required, they are interchanged. - */Wuyi Public Static voidBubblesort (int[] arr) { the for(inti = 0; i < arr.length-1; i++){ - for(intj = 0; J < arr.length-1-I; J + +){ Wu if(Arr[j] > arr[j+1]){ -ARR[J] + = arr[j+1]; AboutARR[J+1] = arr[j]-arr[j+1]; $ARR[J]-= arr[j+1]; - } - } - } A } + the /* - * Hill Sort (Minimum incremental sort) $ * Basic ideas: the * The algorithm first sets the number of groups to be sorted by an increment D (N/2,n is the number of items to be sorted) into groups . the * The difference in subscript between the records in each group D. Direct insertion of all elements in each group the * Then use a smaller increment (D/2) to group it, followed by a direct insert sort in each group. the * When the increment is reduced to 1 o'clock, the sort is completed after the direct insert sort. - */ in Public Static voidShellsort (int[] arr) { the intj = 0; the inttemp = 0; About for(intincrement = ARR.LENGTH/2; Increment > 0; Increment/= 2) { the for(inti = increment; i < arr.length; i++) { thetemp =Arr[i]; the for(j = i; J >= increment; J-=increment) { + if(Temp > Arr[j-Increment]) { -ARR[J] = arr[j-increment]; the}Else{ Bayi Break; the } the } -ARR[J] =temp; - } the } the } the the /* - * Output the */ the Public Static voidOutint[] arr) { theSystem.out.print ("[");94 for(inti = 0; i < arr.length-2; i++){ theSystem.out.print (arr[i]+ ","); the } theSystem.out.println (arr[arr.length-1]+ "]");98 } About -}
Java Common sort