双链表上的快速排序算法

时间:2019-05-02 16:40:09

标签: c# quicksort doubly-linked-list

我已经设法为我的DLL(双向链表)创建了一个有效的quicksort算法,但是,一旦DLL被排序,我似乎找不到停止递归的方法

class Node
{
    public int data;  
    public Node next; //Pointer to next node in DLL 
    public Node prev; //Pointer to previous node in DLL
}

class List
{
    public Node firstNode; //Pointer to head in DLL
}

static void quickSortInit(List sortList)
{
    Node first, head;
    first = head = sortList.firstNode;
    while (head.next != null)
        head = head.next;
    test2(first, head);
}

static void quickSort(Node first, Node head)
{
    if (head != null && first != head && first != head.next)
    {
        Node pivot = quickInsert(first, head);
        quickSort(first, head.prev);
        quickSort(head.next, first);
    }
}

static Node quickInsert(Node first, Node head)
{
    int pivot = head.data;
    int temp;
    Node i = first.prev;

    for (Node current = first; current != head && current != null; current = current.next)
    {
        if (current.data <= pivot)
        {
            if (i == null)
            {
                i = first;
            }
            else
            {
                i = i.next;
            }
            temp = i.data;
            i.data = current.data;
            current.data = temp;
        }
    }
    if (i == null)
    {
        i = first;
    }
    else
    {
        i = i.next;
    }
    temp = i.data;
    i.data = head.data;
    head.data = temp;
    return i;
}

0 个答案:

没有答案