Quick sorting: Package org. rut. util. algorithm. Support; Import org. rut. util. algorithm. sortutil; /** * @ Author treeroot * @ Since 2006-2-2 * @ Version 1.0 */ Public class quicksort implements sortutil. Sort { /* (Non-javadoc) * @ See org. rut. util. algorithm. sortutil. Sort # Sort (INT []) */ Public void sort (INT [] data ){ Quicksort (data, 0, Data. Length-1 ); } Private void quicksort (INT [] data, int I, Int J ){ Int struct tindex = (I + J)/2; // Swap Sortutil. Swap (data, structured tindex, J ); Int K = partition (data, I-1, J, data [J]); Sortutil. Swap (data, K, J ); If (k-I)> 1) quicksort (data, I, k-1 ); If (J-k)> 1) quicksort (data, k + 1, J ); } /** * @ Param data * @ Param I * @ Param J * @ Return */ Private int partition (INT [] data, int L, int R, int partition ){ Do { While (data [++ L] <strong ); While (R! = 0) & Data [-- R]> tables ); Sortutil. Swap (data, L, R ); } While (L <R ); Sortutil. Swap (data, L, R ); Return L; } } Improved quick sorting: Package org. rut. util. algorithm. Support; Import org. rut. util. algorithm. sortutil; /** * @ Author treeroot * @ Since 2006-2-2 * @ Version 1.0 */ Public class improvedquicksort implements sortutil. Sort { Private Static int max_stack_size = 4096; Private Static int Threshold = 10; /* (Non-javadoc) * @ See org. rut. util. algorithm. sortutil. Sort # Sort (INT []) */ Public void sort (INT [] data ){ Int [] stack = new int [max_stack_size]; Int Top =-1; Int timeout; Int struct tindex, L, R; Stack [++ top] = 0; Stack [++ top] = data. Length-1; While (top> 0 ){ Int J = stack [top --]; Int I = stack [top --]; Required tindex = (I + J)/2; Dependencies = data [inittindex]; Sortutil. Swap (data, structured tindex, J ); // Partition L = I-1; R = J; Do { While (data [++ L] <strong ); While (R! = 0) & (data [-- R]> tables )); Sortutil. Swap (data, L, R ); } While (L <R ); Sortutil. Swap (data, L, R ); Sortutil. Swap (data, L, J ); If (l-I)> threshold ){ Stack [++ top] = I; Stack [++ top] = L-1; } If (J-l)> threshold ){ Stack [++ top] = L + 1; Stack [++ top] = J; } } // New insertsort (). Sort (data ); Insertsort (data ); } /** * @ Param data */ Private void insertsort (INT [] data ){ Int temp; For (INT I = 1; I <data. length; I ++ ){ For (Int J = I; (j> 0) & (data [J] <data [J-1]); j --){ Sortutil. Swap (data, J, J-1 ); } } } } |