简体   繁体   English

Java算法-使用ArrayList合并排序

[英]Java Algorithm - Merge Sorting with ArrayLists

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. 我是一名AP计算机科学专业的学生,​​目前的工作是我必须编写一个使用数字ArrayList的程序,并且仅使用标准Java API使用合并排序对其进行排序。 There aren't any compiling errors, but on run-time it doesn't even return the ArrayList! 没有任何编译错误,但在运行时甚至不返回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++;
                }
            }
        }
    }
}

This is because list.size() returns 0 for an empty list. 这是因为list.size()对于空列表返回0。 In your loop where you populate the list, replace list.size() with size . 在填充列表的循环中,将list.size()替换为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. 我尚未检查程序的实际mergeSort部分,但我建议的更改至少将使列表的初始填充生效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM