/**
* @author admin
*/publicclassDown2UpMergeSort<T extendsComparable<T>>extendsMergeSort<T>{publicDown2UpMergeSort(String name){super(name);}@Overrideprotectedvoidsort(T[] nums){int N = nums.length;
aux =(T[])newComparable[N];for(int i =1; i < N; i = i*2){for(int j =0; j < N - i; j += i*2){merge(nums, j, j + i -1, Math.min(j + i + i -1, N -1));}}}}
快速排序
publicclassQuick<T extendsComparable<T>>extendsSort<T>{publicQuick(String name){super(name);}@Overrideprotectedvoidsort(T[] nums){int N = nums.length;sort(nums,0, N -1);}protectedvoidsort(T[] nums,int left,int end){if(end - left <1){return;}int partition =quick(nums, left, end);sort(nums, left, partition -1);sort(nums, partition +1, end);}privateintquick(T[] nums,int left,int end){int i = left;int j = end +1;
T temp = nums[left];while(true){while(less(temp, nums[--j])&& j > i){continue;}while(less(nums[++i], temp)&& j > i){continue;}if(i >= j){break;}swap(nums, i, j);}swap(nums, left, j);return j;}}
三向切分快速排序
/**
* 三向切分快速排序
* 适用于大量重复元素
* @author admin
*/publicclassThreeWayQuick<T extendsComparable<T>>extendsQuick<T>{publicThreeWayQuick(String name){super(name);}@Overrideprotectedvoidsort(T[] nums,int left,int end){if(end - left <1){return;}int lt = left, i = left +1, gt = end;
T temp = nums[left];while(i <= gt){int compare = nums[i].compareTo(temp);if(compare <0){swap(nums, i, lt);
i++;
lt++;}elseif(compare >0){swap(nums, i, gt);
gt--;}else{
i++;}}sort(nums, left, lt -1);sort(nums, gt +1, end);}}