简体   繁体   English

LinkedList的递归Quicksort

[英]Recursive Quicksort for a LinkedList

I've been trying to implement a Recursive Quicksort into my algorithm which makes use of LinkedList. 我一直在尝试在使用LinkedList的算法中实现递归Quicksort。 However when I run the method, it seems to go on forever even for a small list (of 10 elements), I've been waiting for the method to stop for about 10 minutes. 但是,当我运行该方法时,即使只是一小部分(包含10个元素),它似乎也将永远持续下去,我一直在等待该方法停止运行约10分钟。

This is the code in question 这是有问题的代码

public static void QuickSort(LinkedList<Contacto> Lista, int ini, int fin){
    Contacto pivote, aux;
    int i, j, comp;


    if (ini<fin){
        pivote = Lista.get(ini);
        i=ini+1;
        j=fin;

        while (i<j){
            while(i<fin && (comp=Lista.get(i).get_name().compareTo(pivote.get_name()))<=0 )
                i++;

            while((comp=Lista.get(i).get_name().compareTo(pivote.get_name()))>0 )
                j--;

            if(i<j){
                aux = Lista.get(i);
                Lista.set(i, Lista.get(j));
                Lista.set(j, aux);
                }
            }
        aux=Lista.get(j);
        Lista.set(j,pivote);
        Lista.set(ini,aux);
        QuickSort(Lista,ini,j-1);
        QuickSort(Lista,j+1,fin);
    }
}

Thanks for your help! 谢谢你的帮助!

As is noted in the comments, the fact that it's taking ten minutes to sort a list of ten items is due to a bug somewhere, and I recommend you insert some breakpoints/ println() statements to get a sense for how your method is proceeding (one at the top of each of your conditionals should be sufficient to show you where it's getting hung up). 正如评论中指出的那样,对十个项目的列表进行排序需要花费十分钟的时间是由于某个地方的错误所致,我建议您插入一些断点/ println()语句以了解方法的执行方式(每个条件的顶部应该足以显示挂断的位置)。

That said, inefficiency with very short lists is a known problem with the quicksort algorithm - and, if you think about how the algorithm works, you can see why it's an inherent one. 就是说,非常短的列表效率低下是quicksort算法的一个已知问题-而且,如果您考虑一下该算法的工作原理,则可以了解为什么它是固有的。 (I can expound on this, but you'll be better off if you figure out why yourself). (我可以对此进行详细说明,但是如果您弄清楚自己为什么会这样做会更好)。

A common solution to this issue is having a cutoff : When the size of the list becomes smaller than the cutoff size, switch to a simple insertion sort to finish the job. 解决此问题的常见方法是使用截止值 :当列表的大小变得小于截止值时,请切换到简单的插入排序以完成工作。 There is a good discussion about this here , and a very good example implementation here . 有关于这一个很好的讨论在这里 ,和一个很好的例子执行这里

Note the first few lines of the quicksort() method in that example: 请注意该示例中quicksort()方法的前几行:

private static void quicksort(Comparable [] a, int left, int right) {
    final int CUTOFF = 3;
    if (right-left+1 < CUTOFF) { // if less than three elements remain: 
         insertionSort(a,left,right);
    else { //quicksort...

(note: that first link comes from the booksite for Algorithms, 4th edition , written by some folks at Princeton. I can't recommend that site highly enough as you're reasoning through the fundamental algorithms) (注意:第一个链接来自普林斯顿大学一些人编写的第四版算法书的网站。在您通过基本算法进行推理时,我不能推荐该站点足够高)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM