0

I'm an AP Computer Science student and my current assignment is that I have to make a program that takes an ArrayList of numbers and, only using the standard Java API, sort it using a Merge Sort. There aren't any compiling errors, but on run-time it doesn't even return the ArrayList! After a little debugging I found that it isn't populating the original list. Please help! Code:

import java.io.*;
import java.util.*;

public class MergeSort {
    public static void main(String[] args) throws IOException{
    Scanner in  = new Scanner(System.in);
    Random r = new Random();
    int size, largestInt, holder;

    System.out.println("How many integers would you like me to create?");
    size = in.nextInt();
    ArrayList<Integer>list = new ArrayList<Integer>(size);
    System.out.println("What would the largest integer be?");
    largestInt = in.nextInt();

    for(int i = 0; i < list.size(); i++){
        holder = r.nextInt(largestInt + 1);
        list.add(holder);
    }
    mergeSort(list);

    for (int j = 0; j < list.size(); j++) {
        if(j == 19 || j == 39 || j == 59 || j == 79 || j == 99 || j == 119 || j == 139 || j == 159 || j == 179 || j == 199){
            System.out.print(list.get(j));
            System.out.println();
        }
        else{
            System.out.println(list.get(j) + "\t");
        }
    }

}

static void mergeSort(ArrayList<Integer> list) {
    if (list.size() > 1) {
        int q = list.size()/2;
        ArrayList<Integer> leftList = new ArrayList<Integer>();
        for(int i = 0; i > 0 && i <= q; i++){
            leftList.add(list.get(i));
        }
        ArrayList<Integer> rightList = new ArrayList<Integer>();
        for(int j = 0; j > q && j < list.size(); j++){
            rightList.add(list.get(j));
        }

        mergeSort(leftList);
        mergeSort(rightList);
        merge(list,leftList,rightList);
    }
}

static void merge(ArrayList<Integer> a, ArrayList<Integer> l, ArrayList<Integer> r) {
    int totElem = l.size() + r.size();
    int i,li,ri;
    i = li = ri = 0;
    while ( i < totElem) {
        if ((li < l.size()) && (ri<r.size())) {
            if (l.get(li) < r.get(ri)) {
                a.set(i, l.get(li));
                i++;
                li++;
            }
            else {
                a.set(i, r.get(ri));
                i++;
                ri++;
            }
        }
        else {
            if (li >= l.size()) {
                while (ri < r.size()) {
                    a.set(i, r.get(ri));
                    i++;
                    ri++;
                }
            }
            if (ri >= r.size()) {
                while (li < l.size()) {
                    a.set(i, l.get(li));
                    li++;
                    i++;
                }
            }
        }
    }
}
2
  • unnecessary for loops hurt performance. Try to use ArrayList.subList(int fromIndex, int toIndex) which should be faster in the case of the ArrayList. Commented Feb 14, 2014 at 3:44
  • ah, yes. I forgot that ArrayList inherits this. I'll definitely change that. Thank you. Commented Feb 14, 2014 at 4:26

1 Answer 1

1

This is because list.size() returns 0 for an empty list. In your loop where you populate the list, replace list.size() with size.

I haven't checked the actual mergeSort part of your program, but the change that I suggested will at least make the initial population of the list work.

Sign up to request clarification or add additional context in comments.

5 Comments

Awesome it prints! But now it doesn't allow for the proper output. I'm trying to format it where it goes left to right and is spaced using "\t" and goes to the next line. I might change this "if(j%20 == 0){" to just make it every increment of 20 (i.e. 20, 40, 60, 80, etc up to 200).
I suggest java.util.Arrays.toString(T[] array) to print arrays from youList.toArray(~) ... and take a look at Arrays.sort(~) while you are at it. Since your assignement is limited to Java standard API
Ok now the sorting algorithm needs work. It doesn't seem to calculate properly... :I
That might be a reason to post another question, Zombie. Not many people will read it if you just post your question as a comment to my answer here.
@DavidWallace seems a little redundant, but I'll try!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.