使用java中的数组列表合并排序

时间:2013-04-25 12:01:58

标签: java arraylist mergesort

因此,对于家庭作业,我必须编写一个程序,使用与常规数组一起工作的代码中的mergeSorts和数组列表,我只是想知道是否有人可以帮我弄清楚我哪里出错了,因为我的代码抛出了大量的NULL POINTER异常,我试图修复它们,但是当我修复它时它会转到另一个......依此类推....

谢谢! 我的代码:

private static ArrayList<Integer> numbers= new ArrayList<Integer>();
private static ArrayList<Integer> helper;
private static int number;
public static void sort(ArrayList<Integer> myNumbers){
    for(int i=0; i<myNumbers.size();i++){
        numbers.add(myNumbers.get(i));
    }
    //numbers=myNumbers;
    number = myNumbers.size()-1;

    mergesort(0, number -1);
}
private static void mergesort(int low, int high){
    //check if low is smaller than high, if not then the array is sorted
    if(low<high){
        //get the index of the element which is in the middle
        int middle=low+(high-low)/2;
        //sort the left side of the array
        mergesort(low, middle);
        //sort the right side of the array
        mergesort(middle +1, high);
        //combine them both
        merge(low, middle, high);
    }
}
private static void merge(int low, int middle, int high){
    //copy both parts into the helper array
    for(int i=high;i>low;i++){
        helper.add((numbers.get(i)));
    }

    int i=low;
    int j=middle+1;
    int k=low;
    //copy the smallest myNumbers from either the left or right side back to the original array
    while(i<middle  && j<high){
        if(helper.get(i)< helper.get(j)){
            numbers.set(k,(helper.get(i)));
            i++;
        }
        else{
            numbers.set(k,(helper.get(j)));
            j++;
        }
        k++;
    }
    //copy the rest of the left side of the array into target array
    while(i<middle){
        numbers.set(k,helper.get(i));
        k++;
        i++;
    }
}

返回:

Exception in thread "main" java.lang.NullPointerException
at BinarySearch.merge(BinarySearch.java:61)
at BinarySearch.mergesort(BinarySearch.java:55)
at BinarySearch.mergesort(BinarySearch.java:51)
at BinarySearch.mergesort(BinarySearch.java:51)
at BinarySearch.mergesort(BinarySearch.java:51)
at BinarySearch.mergesort(BinarySearch.java:51)
at BinarySearch.mergesort(BinarySearch.java:51)
at BinarySearch.mergesort(BinarySearch.java:51)
at BinarySearch.sort(BinarySearch.java:43)
at BinarySearch.main(BinarySearch.java:25)

5 个答案:

答案 0 :(得分:1)

它尝试访问未创建的合并函数中的辅助字段。

此外,我认为您应该从所有功能/字段中删除静态关键字。

private static ArrayList helper;

private static ArrayList helper = new ArrayList();

答案 1 :(得分:1)

这是罪魁祸首:

for(int i=high;i>low;i++){
    helper.add((numbers.get(i)));
}

改为使用for(int i=high; i>=low; i--) {

答案 2 :(得分:0)

blackuprise指出了你的两个问题。

您的合并实现还有另一个问题,可能会导致合并错误。在合并方法结束时,您有:

//copy the rest of the left side of the array into target array
while...

你怎么能确定每次合并阵列的所有右边,只留下左边的元素? (奇怪的句子^ _ ^左边的部分有......左。)

您也应该检查右侧。或使用SENTINEL来避免“休息”检查。

答案 3 :(得分:0)

如果您尝试使用您的算法实现合并排序以获得学术用途,那么给定的答案是好的,否则如果您对Guava已有的实现感兴趣,那么它是:{{3} }

答案 4 :(得分:0)

public class Merged

public  static void sort(List<String>result )
{

     for( i=0;i<result.size();i++)
     {
        try{

          try{


         if(result.get(i).compareTo(result.get(i+1))>0)
             {
             String aux=result.get(i);
             result.set(i, result.get(i+1));
             result.set(i+1, aux);

             }


             }catch(NullPointerException e){}
          }catch(ClassCastException z){}

        }
    }


public static void main(String[]args) throws ClassNotFoundException
{ 
    String[]resultentry=new String[100];
    int index;//nr de elemente din vectorul de stringuri//
    int i;

    try{
   //declare files and lists//
    BufferedReader firstfile;
    BufferedReader secondfile;


    firstfile=new BufferedReader(new FileReader("lista1.txt"));
    secondfile=new BufferedReader(new FileReader("lista2.txt"));;


         firstentry=firstfile.readLine();
        secondentry=secondfile.readLine();

        while(firstentry!=null&&secondentry!=null)
        {
        firstlist.add(firstentry);
        firstentry=firstfile.readLine();

        secondlist.add(secondentry) ;
        secondentry=secondfile.readLine();

         }


     }catch(IOException exp){}


     try{

    java.util.Collections.sort(firstlist);
    System.out.println("First list sorted :"+ firstlist);

    java.util.Collections.sort(secondlist);
    System.out.println("Second list sorted"+secondlist);


    firstlist.addAll(secondlist);
    java.util.Collections.sort(firstlist);

    System.out.println("Result  list sorted "+" "+firstlist);

    }catch(ClassCastException b){}

   }

}`