Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 对字符串数组列表进行快速排序_Java_Quicksort - Fatal编程技术网

Java 对字符串数组列表进行快速排序

Java 对字符串数组列表进行快速排序,java,quicksort,Java,Quicksort,我现在有这个,但我相信它是用来排序整数的,但我需要排序字符串。如何将其更改为适用于字符串 /** * This method should use a quick sort approach to rearrange * the references in the ArrayList 'list' such that they are in * non-decreasing alphabetic order. * * @param list An ArrayList of Vehicle ob

我现在有这个,但我相信它是用来排序整数的,但我需要排序字符串。如何将其更改为适用于字符串

/**
* This method should use a quick sort approach to rearrange
* the references in the ArrayList 'list' such that they are in
* non-decreasing alphabetic order.
* 
* @param list An ArrayList of Vehicle objects that need sorting
* @return  The ArrayList of vehicles that has been sorted using quick sort
*/
protected ArrayList<Vehicle> quickSort(ArrayList<Vehicle> list)
{
    // Use the quick sort algorithm to sort 'vehicles' and then 
    // return it. Initially this method just returns an empty
    // list - you need to fix this.
    ArrayList<Vehicle> sorted = new ArrayList<Vehicle>();
    Vehicle smaller = new Vehicle();
    Vehicle greater = new Vehicle();
    int pivot = list.get(0);
    int i;
    int j;
    for (i = 1; i < list.size(); i++) {
        j = list.get(i);
        if (j.compareTo(pivot) < 0) {
            smaller.add(j);
        } else {
            greater.add(j);
        }
    }
    smaller = quicksort(smaller);
    greater = quicksort(greater);
    smaller.add(pivot);
    smaller.addAll(greater);
    smaller = sorted;

    return sorted;
}

一些解释补充说:

public static ArrayList<Vehicle> quickSort(ArrayList<Vehicle> list)
{
    if (list.isEmpty()) 
        return list; // start with recursion base case
    ArrayList<Vehicle> sorted;  // this shall be the sorted list to return, no needd to initialise
    ArrayList<Vehicle> smaller = new ArrayList<Vehicle>(); // Vehicles smaller than pivot
    ArrayList<Vehicle> greater = new ArrayList<Vehicle>(); // Vehicles greater than pivot
    Vehicle pivot = list.get(0);  // first Vehicle in list, used as pivot
    int i;
    Vehicle j;     // Variable used for Vehicles in the loop
    for (i=1;i<list.size();i++)
    {
        j=list.get(i);
        if (j.compareTo(pivot)<0)   // make sure Vehicle has proper compareTo method 
            smaller.add(j);
        else
            greater.add(j);
    }
    smaller=quickSort(smaller);  // capitalise 's'
    greater=quickSort(greater);  // sort both halfs recursively
    smaller.add(pivot);          // add initial pivot to the end of the (now sorted) smaller Vehicles
    smaller.addAll(greater);     // add the (now sorted) greater Vehicles to the smaller ones (now smaller is essentially your sorted list)
    sorted = smaller;            // assign it to sorted; one could just as well do: return smaller

    return sorted;
}

较小=已排序;可能应该分类=更小;它不是用于排序字符串,而是用于排序车辆实例。更小和更大的应该是车辆列表,而不是车辆。pivot和j应该是Vehicle类型的。你能详细解释一下吗?Schwobasegl不太明白你的意思。你试过编译你的代码吗?我在int pivot=list.get0;对于不兼容的类型