QuickSort on Doubly Linked List

前端 未结 3 848
独厮守ぢ
独厮守ぢ 2021-01-21 10:46

I want to implement the QuickSort Algorithm on a sync Doubly Linked List. I give the function \"partition\" the left and right border, then it starts to search lower values on t

相关标签:
3条回答
  • 2021-01-21 11:15

    Here is an implementation for QuickSort using a DoublyLinkedList class which contains a reference to the first (in.first) ListElement, a list element contains a key and prev and next references:

    public DoublyLinkedList quicksort(DoublyLinkedList in, int numOfElements) {
        in.first = partition(in.first, in.first.prev);
        return in;
    }
    
    private ListElement partition(ListElement first, ListElement pivotElement) {
        ListElement left = first;
        ListElement right = pivotElement;
    
        while (left != pivotElement) {
            if (left.getKey() > pivotElement.getKey()) {
                ListElement next = left.next;
                if (left == first)
                    first = next;
                //Remove currentLeft
                left.prev.next = left.next;
                left.next.prev = left.prev;
    
                //Connect with element after currentRight
                left.next = right.next;
                right.next.prev = left;
    
                //Connect with pivotElement
                left.prev = right;
                right.next = left;
    
                right = left; //Switch right with left, since we just swapped their positions
                left = next;  //Assign left to the next object (from the left part)
            } else {
                left = left.next;
            }
        }
        if (first != pivotElement.prev && first != pivotElement)
            first = partition(first, pivotElement.prev);
        if (pivotElement != right)
            partition(pivotElement.next, right);
        return first;
    }
    

    At the time of this writing, when I run this on my desktop computer with a very recent Haswel CPU I get the following results:

    Quicksort:
    1.000.000 Items: 696ms
    8.300.000 Items: 8131ms

    Note that this is much slower than my MergeSort implementation for the same data structure, for which I get the following timings on the same computer with the same input:

    Mergesort:
    1.000.000 Items: 466ms
    8.300.000 Items: 5144ms

    Note the timings are specific to my hardware and you might get different results.

    0 讨论(0)
  • 2021-01-21 11:24

    Just from a quick skim, it seems that your list is not only doubly linked, but also is connected at the ends (so it's more like a Ring than like a list). In other words, if I were to iterate over your list (containing elements A, B, C, D), it wouldn't be:

    A -> B -> C -> D -> stop
    

    Instead it would be

    A -> B -> C -> D -> A -> B -> C -> D -> A -> B ..... etc.
    

    I suspect that could be why you are having an infinite loop.

    I would create a reference to the last element of your list in your DoublyLinkedList class (example: in.last), use that for getting the last element, and have the first and last elements link to either null or some sort of NullListElement extends ListElement


    If you must keep it as a ring, I will still add a reference to the last element of your list, so that you can say something like:

    if(walker == in.last) break; // stop
    
    0 讨论(0)
  • 2021-01-21 11:24
    Node partition(Node start, Node end){
        Node l = start;
        Node h = end.previous;
        Node pivot = end;
    
        if(end!=null && end.next!=start){ //Whenever deal with end.next, check null check
            while(h!=null && h.next!=l){//Whenever deal with end.next, check null check
                System.out.println("gumi");
                if(l.data<pivot.data)
                    l=l.next;
                else if(h.data>pivot.data)
                    h=h.previous;
                else{
                    int temp = l.data;
                    l.data = h.data;
                    h.data = temp;
                }   
            }   
            int temp = pivot.data;
            pivot.data = l.data;
            l.data = temp;
        }
        return l;
    
    }
    void quicksort(Node start, Node end){
         System.out.println("jose");
       if(end!=null && end.next!=start ){ //Whenever deal with end.next, check null check , end should not be less than start: so the condition end.next!=start 
           System.out.println("Quixk");
           Node pivot = partition(start,end);
           quicksort(start, pivot.previous);
           quicksort(pivot.next, end);
       }
    
    }
    
    0 讨论(0)
提交回复
热议问题