QuickSort on Doubly Linked List

前端 未结 3 859
独厮守ぢ
独厮守ぢ 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: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
    

提交回复
热议问题