如何打印出我的合并排序ArrayList?

时间:2016-02-17 01:39:03

标签: java arraylist

我已经完成了我的任务,但我似乎无法弄清楚如何打印出我的排序数组列表。它目前只打印出20个随机数的原始数组列表。我将打印放在merge或mergeSort方法中吗?或者我完全做其他事情?任何帮助将不胜感激。

//Conner Tilley
import java.util.*;

public class BuiltIns
{
    public static void main(String[] args) {

            //Built in setup
        ArrayList<Integer> a = new ArrayList<Integer>();    
        Queue<Integer> q = new LinkedList<Integer>();
        Random r = new Random();

        //populate loop
        for(int j=1;j<=20;j++)
        {
            int fill = r.nextInt(100);
            a.add(new Integer(fill));
        }

        //check and some display
        int size = a.size();
        System.out.println("Populating the Array List of Size 20");
        System.out.println("This list contains");
        System.out.println(a);
        System.out.println("Sorting");
        //actual merging
        System.out.println(a);
    }

    //first merging method
    public static void mergeSort(int[] a)
    {
        int size = a.length;
        if(size<2)//halt and then return
        {
            return;
        }

        //some initial setup
        int mid = size/2;
        int rightSize = size-mid;
        int leftSize = mid;
        int[] left = new int[leftSize];
        int[] right = new int[rightSize];

        //populate the left
        for(int i=0;i<mid;i++)
        {
            left[i] = a[i];
        }

        //populate the right
        for(int i=mid;i<size;i++)
        {
            right[i-mid] = a[i];
        }
            mergeSort(left);
            mergeSort(right);
            //actual merging
            merge(left,right,a);
            System.out.println(a);
    }

    //second merging method
    public static void merge(int[] left, int[] right, int[] a)
    {
        int leftSize = left.length;
        int rightSize = right.length;
        int i=0;//left
        int j=0;//right
        int k=0;//a

        /*
         * while loop to compare the first index of two arrays
         * and order them until complete
         */
        while(i<leftSize && j<rightSize)
        {
            if(left[i]<=right[j])
            {
                a[k] = left[i];
                i++;
                k++;
            }
            else
            {
                a[k] = right[j];
                j++;
                k++;
            }
        }
        //This is used to fill in the rest
        while(i<leftSize)
        {
            a[k] = left[i];
            i++;
            k++;
        }
        while(j<rightSize)
        {
            a[k] = right[j];
            j++;
            k++;
        }
        System.out.println(a);
    }

}

1 个答案:

答案 0 :(得分:0)

由于数组是引用数据类型,因此可以在调用merge方法之前和之后打印数组。最好在merge方法之外打印它,因为通常使用合并方法的人不希望打印数组。

所以在你的主要方法中你可以这样做:

    //check and some display
    System.out.println("Populating the Array List of Size 20");
    System.out.println("This list contains:");
    //extracting an array from the list
    int[] arrFromList = new int[a.size()]; 
    for(int i = 0; i < a.size(); i++) {
        arrFromList[i] = a.get(i);
    }   
    //printing the unsorted array
    System.out.println(Arrays.toString(arrFromList));
    System.out.println("Sorting");
    //actual merging
    mergeSort(arrFromList);
    System.out.println("Sorted array:");
    //printing the sorted array
    System.out.println(Arrays.toString(arrFromList));