[英]Merge sort with array lists in java
因此,對於家庭作業,我必須編寫一個程序,將來自與常規數組一起使用的代碼中的數組列表進行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)
它嘗試訪問未創建的合並函數中的輔助字段。
另外我認為你應該從所有函數/字段中刪除static關鍵字。
private static ArrayList helper;
至
private static ArrayList helper = new ArrayList();
這是罪魁禍首:
for(int i=high;i>low;i++){
helper.add((numbers.get(i)));
}
for(int i=high; i>=low; i--) {
代替。
blackuprise指出了你的兩個問題。
您的合並實現還有另一個問題,可能會導致合並結果錯誤。 在合並方法結束時,您有:
//copy the rest of the left side of the array into target array
while...
你怎么能確定每次合並陣列的所有右側,並且只有左側部分有剩余的元素? (奇怪的句子^ _ ^左邊的部分有......左。)
您也應該檢查右側。 或者使用SENTINEL
來避免“休息”檢查。
如果您正在嘗試使用您的算法實現合並排序以獲得學術用途,那么給定答案是好的,否則如果您對Guava已經具有的實現感興趣,那么它是: guava Iterators
公共類合並
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){}
}
}`
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.