Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//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, Comparator<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, comp, begin, split);
        sortRecursive(array, comp, split, end);
        //Create indexes for storing sorting data
        int[] indexA = new int[end - begin];
        int[] indexB = new int[end - begin];
        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 = array[counterLeft];
            T b = array[counterRight];
            //Compares values, then adds the smaller
            //one to the indexes
            if (comp.compare(a, b) > 0) {
                indexA[indexCounter] = counterRight - begin;
                indexB[counterRight++ - begin] = indexCounter++;
                if (counterRight >= end) {
                    //When one array is complete,
                    //add the remainder of the other to the index
                    while (counterLeft < split) {
                        indexA[indexCounter] = counterLeft - begin;
                        indexB[counterLeft++ - begin] = indexCounter++;
                    }
                    complete = true;
                }
            } else {
                indexA[indexCounter] = counterLeft - begin;
                indexB[counterLeft++ - begin] = indexCounter++;
                if (counterLeft >= split) {
                    //When one array is complete,
                    //add the remainder of the other to the index
                    while (counterRight < end) {
                        indexA[indexCounter] = counterRight - begin;
                        indexB[counterRight++ - begin] = indexCounter++;
                    }
                    complete = true;
                }
            }
        }
        //Swaps the elements in the array such that the array is sorted
        for (int i = 0; i < end - begin; i++) {
            int target = indexA[i];
            T a = array[begin + target];
            T b = array[begin + i];
            array[begin + target] = b;
            array[begin + i] = a;
            int ai = indexB[target];
            int bi = indexB[i];
            indexB[target] = bi;
            indexB[i] = ai;
            indexA[bi] = target;
        }
        //The array should now be sorted
    }

    private static <T> void sortRecursive(List<T> array, 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.get(begin);
            T b = array.get(begin + 1);
            if (comp.compare(a, b) > 0) {
                array.set(begin, b);
                array.set(begin + 1, a);
            }
            return;
        }
        //Split the array into half
        int split = (end + begin) / 2;
        //Sort each half of the array
        sortRecursive(array, comp, begin, split);
        sortRecursive(array, comp, split, end);
        //Create indexes for storing sorting data
        int[] indexA = new int[end - begin];
        int[] indexB = new int[end - begin];
        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 = array.get(counterLeft);
            T b = array.get(counterRight);
            //Compares values, then adds the smaller
            //one to the indexes
            if (comp.compare(a, b) > 0) {
                indexA[indexCounter] = counterRight - begin;
                indexB[counterRight++ - begin] = indexCounter++;
                if (counterRight >= end) {
                    //When one array is complete,
                    //add the remainder of the other to the index
                    while (counterLeft < split) {
                        indexA[indexCounter] = counterLeft - begin;
                        indexB[counterLeft++ - begin] = indexCounter++;
                    }
                    complete = true;
                }
            } else {
                indexA[indexCounter] = counterLeft - begin;
                indexB[counterLeft++ - begin] = indexCounter++;
                if (counterLeft >= split) {
                    //When one array is complete,
                    //add the remainder of the other to the index
                    while (counterRight < end) {
                        indexA[indexCounter] = counterRight - begin;
                        indexB[counterRight++ - begin] = indexCounter++;
                    }
                    complete = true;
                }
            }
        }
        //Swaps the elements in the array such that the array is sorted
        for (int i = 0; i < end - begin; i++) {
            int target = indexA[i];
            T a = array.get(begin + target);
            T b = array.get(begin + i);
            array.set(begin + target, b);
            array.set(begin + i, a);
            int ai = indexB[target];
            int bi = indexB[i];
            indexB[target] = bi;
            indexB[i] = ai;
            indexA[bi] = target;
        }
        //The array should now be sorted
    }
}