使用带有覆盖比较的quickSort

时间:2015-12-29 03:13:36

标签: java sorting quicksort

我试图找出为什么我得到NullPointerException,尽管数组中填充了卡对象。我问的是,我的QuickSort算法可以使用我在卡片类中覆盖的比较方法吗?

快速排序:

package sort;

//All import goes here
import java.util.ArrayList;
import java.util.Comparator;

//Comment out at the moment
//import java.util.List;



public class QuickSort<T> {

    private ArrayList<T> list;
    private int length;
    Comparator<T> pod;



    public void sort(ArrayList<T> lst){

        //Check if the list is empty
        //if(lst.isEmpty() || lst == null){
        if(lst == null || lst.isEmpty()){
             return;
        }
        this.list = lst;
        length = lst.size();
        quickSort(0, length - 1);

    }

    private void quickSort(int lowIndex, int highIndex){
        int low = lowIndex;
        int high = highIndex;

        //Find the pivot value
        T pivot = list.get(lowIndex + (highIndex - lowIndex)/2);

        while(low <= high){
            /**
             * In each iteration, we will find a value from the left side 
             * which is greater than the pivot value, and also we will
             * find a value from the right side which is less then the pivot 
             * once the search is done, we will then exchange both numbers 
             */

            //o1 < pivot
            while(pod.compare(list.get(low), pivot) == -1){

                low++;

            }

            //o1 > pivot
            while(pod.compare(list.get(high), pivot) == 1){
                high--;
            }

            if(low <= high){
                swap(low, high);

                //Move index to next position on both side 
                low++;
                high--;
            }
        }

        //Call quickSort recursively
        if(lowIndex < high){
            quickSort(lowIndex, high);
        }
        if(low < highIndex){
            quickSort(low, highIndex);
        }

    }

    private void swap(int i, int j){
        //Temporary variable to hold the element at index i 
        T temp = list.get(i);
        //Swap i index with the element in j index
        list.set(i, list.get(j));
        //Swap j index with the element that was in i index
        list.set(j, temp);
    }
}

卡:

//Compare method in my Card class
public class Card implements Comparator<Card>{
    .....

    @Override
    public int compare(Card o1, Card o2) {

        //Check if o1 has the highest suit
        if(o1.getSuit().getVal() > o2.getSuit().getVal()){
            return 1;
        }

        //Check if o2 has highest suit
        else if(o1.getSuit().getVal() < o2.getSuit().getVal()){
            return -1;
        }

        //Check if the suit are the same
        else if(o1.getSuit().equals(o2.getSuit())){

            //Check if o1 has the highest value
            if(o1.getValue().getVal() > o2.getValue().getVal()){
                return 1;
            }

            //Check if o2 has the highest value
            else if(o1.getValue().getVal() > o2.getValue().getVal()){
                return -1;
            }
        }
        //Either there duplicate or something wrong with the compare method. 
        System.out.println("Well there something wrong with the card Compare method or there duplicate of card in the deck.");
        return 0;
    }
}

铲:

//Sort player hand for easy readability
QuickSort<Card> sort = new QuickSort<Card>();
System.out.println(USER.getHand().size());
sort.sort(USER.getHand());

错误:

Exception in thread "main" java.lang.NullPointerException
at sort.QuickSort.quickSort(QuickSort.java:48)
at sort.QuickSort.sort(QuickSort.java:28)
at Spade.main(Spade.java:155)

更新:

我检查引发异常的compare方法的参数是否为null,两个参数检查都返回false

1 个答案:

答案 0 :(得分:0)

我看到两个空指针异常的地方:

line 16: pod is not initialized, so it is causing an error at line 48

line 23: test for null before testing isEmpty 

泛型类型T必须实现可比较,然后使用pivot.compareTo

看看这个问题,这是一个通用的冒泡排序。 Generic arraylist bubblesort problems