public static void mergeSort ( final int from , final int to , final IntComparator c , final Swapper swapper ) { /* * We retain the same method signature as quickSort. Given only a comparator and swapper we * do not know how to copy and move elements from/to temporary arrays. Hence, in contrast to * the JDK mergesorts this is an "in-place" mergesort, i.e. does not allocate any temporary * arrays. A non-inplace mergesort would perhaps be faster in most cases, but would require * non-intuitive delegate objects... */ final int length = to - from ; // Insertion sort on smallest arrays if ( length < MERGESORT_NO_REC ) { for ( int i = from ; i < to ; i ++ ) { for ( int j = i ; j > from && ( c . compare ( j - 1 , j ) > 0 ) ; j -- ) { swapper . swap ( j , j - 1 ) ; } } return ; } // Recursively sort halves int mid = ( from + to ) >>> 1 ; mergeSort ( from , mid , c , swapper ) ; mergeSort ( mid , to , c , swapper ) ; // If list is already sorted, nothing left to do. This is an // optimization that results in faster sorts for nearly ordered lists. if ( c . compare ( mid - 1 , mid ) <= 0 ) return ; // Merge sorted halves inPlaceMerge ( from , mid , to , c , swapper ) ; }
Sorts the specified range of elements using the specified swapper and according to the order induced by the specified comparator using mergesort .