Java tutorial
//package com.java2s; //License from project: Open Source License import java.util.Comparator; import java.util.List; public class Main { private static <T> void sortRecursive(T[] array, T[] unsorted, Comparator<? super T> comp, int begin, int end) { //A sorting section with no elements or one element needs not be sorted if (begin == end || begin == end - 1) { return; } //A sorting section with two elements can be sorted trivially //Swap if the larger element precedes the smaller if (begin == end - 2) { T a = array[begin]; T b = array[begin + 1]; if (comp.compare(a, b) > 0) { array[begin] = b; array[begin + 1] = a; } return; } //Split the array into half int split = (end + begin) / 2; //Sort each half of the array sortRecursive(array, unsorted, comp, begin, split); sortRecursive(array, unsorted, comp, split, end); int indexCounter = 0; int counterLeft = begin; int counterRight = split; boolean complete = false; //Merging the arrays //Fills in the indexes; one plots the final position of //the element to the element's index; the other plots the //element at the given index to its final position while (!complete) { T a = unsorted[counterLeft]; T b = unsorted[counterRight]; //Compares values, then adds the smaller //one to the indexes if (comp.compare(a, b) > 0) { array[begin + indexCounter++] = b; counterRight++; if (counterRight >= end) { //Copy the remaining values System.arraycopy(unsorted, counterLeft, array, begin + indexCounter, end - begin - indexCounter); complete = true; } } else { array[begin + indexCounter++] = a; counterLeft++; if (counterLeft >= split) { //Copy the remaining values System.arraycopy(unsorted, counterRight, array, begin + indexCounter, end - begin - indexCounter); complete = true; } } } //The array should now be sorted } private static <T> void sortRecursive(List<T> list, List<T> unsorted, Comparator<? super T> comp, int begin, int end) { //A sorting section with no elements or one element needs not be sorted if (begin == end || begin == end - 1) { return; } //A sorting section with two elements can be sorted trivially //Swap if the larger element precedes the smaller if (begin == end - 2) { T a = list.get(begin); T b = list.get(begin + 1); if (comp.compare(a, b) > 0) { list.set(begin, b); list.set(begin + 1, a); } return; } //Split the array into half int split = (end + begin) / 2; //Sort each half of the array sortRecursive(list, unsorted, comp, begin, split); sortRecursive(list, unsorted, comp, split, end); int indexCounter = 0; int counterLeft = begin; int counterRight = split; boolean complete = false; //Merging the arrays //Fills in the indexes; one plots the final position of //the element to the element's index; the other plots the //element at the given index to its final position while (!complete) { T a = unsorted.get(counterLeft); T b = unsorted.get(counterRight); //Compares values, then adds the smaller //one to the indexes if (comp.compare(a, b) > 0) { list.set(begin + indexCounter++, b); counterRight++; if (counterRight >= end) { //Copy the remaining values while (counterLeft < split) { list.set(begin + indexCounter++, unsorted.get(counterLeft++)); } complete = true; } } else { list.set(begin + indexCounter++, a); counterLeft++; if (counterLeft >= split) { //Copy the remaining values while (counterRight < end) { list.set(begin + indexCounter++, unsorted.get(counterRight++)); } complete = true; } } } //The list should now be sorted } }