java - 使用带有覆盖比较的快速排序

标签 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)

更新:

我检查异常发生处的比较方法的参数是否为空,两个参数检查都返回 false

最佳答案

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

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

line 23: test for null before testing isEmpty 

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

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

关于java - 使用带有覆盖比较的快速排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34503883/

相关文章:

c++ - C++ STL 中 SORT 的自定义比较函数?

c++ - 为什么快速排序在重复元素多的情况下效率低下?

c - 快速排序中的 Lomuto-Partition

clojure - 如何具体化 Prolog 的回溯状态以执行与 Clojure 中的 "lazy seq"相同的任务?

java - 使用新主键克隆现有对象

java - db4o 类模型 transient 场

python - 有没有办法在 python 中简化这个 "n-way merge"

C++ 类输出和排序

java - 创建Session时getCurrentSession返回null

java - 查找java中方法的执行时间