Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.List;

public class Main {
    public static void doMerge(List<Integer> list1, int start, int end) {
        int mid;
        if (start == end)
            return;
        mid = (start + end) / 2;

        doMerge(list1, start, mid);
        doMerge(list1, mid + 1, end);

        doMergeSort(list1, start, mid, mid + 1, end);
    }

    public static void doMergeSort(List<Integer> list1, int start1, int end1, int start2, int end2) {

        int[] temp = new int[list1.size()];
        int k = 0, i = start1;
        while (start1 <= end1 && start2 <= end2) {
            if (list1.get(start1) == list1.get(start2)) {
                temp[k] = list1.get(start1);
                k++;
                start1++;
                temp[k] = list1.get(start2);
                k++;
                start2++;
            } else if (list1.get(start1) > list1.get(start2)) {
                temp[k] = list1.get(start1);
                k++;
                start1++;
            } else if (list1.get(start1) < list1.get(start2)) {
                temp[k] = list1.get(start2);
                k++;
                start2++;
            }
        }
        if (start1 <= end1) {
            for (; start1 <= end1; start1++, k++) {
                temp[k] = list1.get(start1);
            }
        }
        if (start2 <= end2) {
            for (; start2 <= end2; start2++)
                temp[k] = list1.get(start2);
        }
        for (int m = 0; m < k; m++, i++) {
            list1.remove(i);
            list1.add(i, temp[m]);
        }
    }
}