java中的递归合并排序

时间:2015-01-04 14:08:17

标签: java recursion merge queue mergesort

我一直在研究合并排序作为算法和可视化。当我完成编码部分时,对我来说有点挑战。但是,我仍然使用运行时错误进行编码。错误是 NullPointerException 。请帮我。这是一个学校项目。

public class test
{       
    public static void main (String[]args)
    {               
        int arr[]={11,34,65,89,1,456,90,85,12,70};
        mergesort(arr,0,9);
        for(int a=0;a<arr.length;a++)
            System.out.println(arr[a]);
    }

    public static void mergesort (int arr[], int low, int high)
    {           
        int middle;
        if(low<high)
        {
        middle = (low+high)/2;
        mergesort(arr,low,middle);
        mergesort(arr,middle+1,high);
        merge(arr,low,middle,high);
        }           
    }

    public static void merge(int arr[], int low, int middle, int high)
    {
        Queue<Integer> buffer1 = new LinkedList<Integer>();     
        Queue<Integer> buffer2 = new LinkedList<Integer>(); 
        int i;

        for(int a=low;a<middle;a++) buffer1.add(arr[a]);
        for(int a=middle+1;a<high;a++) buffer2.add(arr[a]);
        i=low;
        while(!(buffer1.isEmpty()) && !(buffer2.isEmpty()))
        {
            if(buffer1.peek() <= buffer2.peek())
            {
                arr[i] = buffer1.poll();

            } else
            {
                arr[i] = buffer2.poll();                    
            }               
        }
        while(!(buffer1.isEmpty()))
        {
            arr[i] = buffer1.poll();
        }
        while(!(buffer2.isEmpty()))
        {
            arr[i] = buffer2.poll();
        }
    }

}

2 个答案:

答案 0 :(得分:0)

像这样更改你的for循环

for(i=low;i<=middle;i++)
{
    buffer1.add(arr[i]);                
}
for(i=middle+1;i<=high;i++)
{
    buffer2.add(arr[i]);
}

此处 low high 表示第一个 last 元素的索引。

答案 1 :(得分:0)

这是更新的代码,运行正常,没有编译和运行时错误:

public class Merge 
{

public static void mergesort(int arr[], int low, int high)
{
    int middle;
    if(low<high)
    {
        middle = (low+high)/2;
        mergesort(arr,low,middle);
        mergesort(arr,middle+1,high);
        merge(arr,low,middle,high);
    }
}
public static void merge(int arr[], int low, int middle, int high)
{
    Queue<Integer>half1 = new LinkedList<Integer>();
    Queue<Integer>half2 = new LinkedList<Integer>();

    for(int j = low; j<=middle;j++)
        {               
            half1.add(arr[j]);
        }
    for(int j= middle+1 ; j<=high ; j++)
        {
            half2.add(arr[j]);
        }
    int i = low;
    while(!(half1.isEmpty() || half2.isEmpty()))
    {
        if(half1.peek() <= half2.peek())
        {
            arr[i++] = half1.poll();

        }
        else {arr[i++] = half2.poll();}
    }

    while(!half1.isEmpty())
    {
        arr[i++] = half1.poll();
    }
    while(!half2.isEmpty())
    {
        arr[i++] = half2.poll();
    }
}

    public static void main (String[]args)
    {
         int arr[] = {11,45,90,121,67,19,54,28,7,50}; 
         mergesort(arr,0,arr.length-1);

         for(int a=0;a<arr.length;a++)
              System.out.println(arr[a]);
    }
 }