mardi 26 mars 2019

Mergesort Sometimes Returning Wrong List- Java

I am writing a mergesort in Java. However, it is currently not always printing the correct order of passed integers in the List. Where did I go wrong exactly?

I have tried changing the size of the List (currently at 10 - variable arrayLength). Seemed to be fine for length 2, but I need very large sorted lists (n > 1000).

public class MergeSort {

static LinkedList<Integer> list = new LinkedList<>();

private static LinkedList<Integer> sortedList;
static int randomInt;

public static void main(String[] args) {


    fillArray();
    sortedList = mergeSort(list);
    printSortedList();
}

private static void fillArray() {

    int arrayLength = 10;
    for(int i = 0; i < arrayLength; i++ ) {
        randomInt = (int) (Math.random()*arrayLength);
        list.add(i, randomInt);
    }
}

private static LinkedList mergeSort(LinkedList<Integer> list) {


    int middle;
    LinkedList<Integer> leftHalf = new LinkedList<Integer>();
    LinkedList<Integer> rightHalf = new LinkedList<Integer>();


    if(list.size() == 1) {
        return list;
    }
    else {
        //split list in half (round if uneven).
        //Then, add the left half of the array to a new sub-array
        middle = list.size()/2;
        for(int k = 0; k < middle; k++) {
            leftHalf.add(list.get(k));
        }
        //repeat for right side but with total size
        for(int j = middle; j < list.size(); j++) {
            rightHalf.add(list.get(j));
        }

        //Recursively call mergeSort to split even further
        //Set new values of left + right
        leftHalf = mergeSort(leftHalf);
        rightHalf = mergeSort(rightHalf);

        //Now, with obtained current arrays, sort them
        mergeAll(leftHalf, rightHalf, list);
    }
    return list;
}

private static void mergeAll(LinkedList<Integer> leftHalf, LinkedList<Integer> rightHalf, LinkedList<Integer> list) {

    //Initialize to be used indices

    int left = 0;
    LinkedList<Integer> remainderArray;
    int remainderIndex;
    int indexList = 0; //index for whole list
    int right = 0;

    //Change index elements depending on size
    while (left < leftHalf.size() && right < rightHalf.size()) {
        if ( (leftHalf.get(left).compareTo(rightHalf.get(right))) < 0) {
            list.set(indexList, leftHalf.get(left));
            left++;
        } else {
            list.set(indexList, rightHalf.get(right));
            right++;
        }
        indexList++;
    }
    //Now we check if there are any elements left to be checked
    //in the left and right arrays respectively
    if(left >= leftHalf.size()) {
        remainderArray = rightHalf;
        remainderIndex = right;
    }
    else {
        remainderArray = leftHalf;
        remainderIndex = left;
    }

    //Now, either left or right is empty with one still containing elements
    for(int k = remainderIndex; k < remainderArray.size(); k++) {
        list.set(indexList, remainderArray.get(k));
        remainderIndex++;
    }
}

public static void printSortedList() {
    for(int i=0; i< sortedList.size(); i++ ) {
        System.out.println(sortedList.get(i));
    }
}
}

I want to print the array, ascending. Current output would be something like:

0 0 1 5 6 6 7 0 6

Which is clearly wrong, because of the 0 before the last element.

Thanks!




Aucun commentaire:

Enregistrer un commentaire