合并排序不会拆分列表的右侧

时间:2015-05-11 21:16:19

标签: c++ sorting mergesort

与标题状态一样,我的合并排序仅分割左侧。其他一切工作正常,包括我的合并功能。我无法弄清楚原因。我真的很感激帮助。在包含以下内容的列表中:7,4,10,2,6,1,3,7,11,5输出1,2,3,4,6,7,7,10,11,5

编辑:补充了我班上的其余成员。

#include <iostream>
#include <string>
using namespace std;

class linkedList
{
private:
    class node
    {
    public:
        int data;
        node * next;
        node * prev;


    node(int x)
    {
        data = x;
        next = NULL;
        prev = NULL;
    }
};

node * head;
node * tail;

node * split()
{
    node * finger = head;
    node * fast = head->next;
    while (fast != NULL)
    {
        fast = fast->next;
        if (fast != NULL)
        {
            fast = fast->next;
            finger = finger->next;
        }
    }
    tail = finger->next;
    node * splitB = tail;
    splitB->prev = NULL;
    finger->next = NULL;
    return splitB;
}

node * merge(node * a, node * b)
{
    linkedList m;

    while(a != NULL || b != NULL)
    {
        if(b == NULL)
        {
            if(m.head != NULL)
            {
                a->prev = m.tail;
                m.tail->next = a;
                m.tail = a;

            }
            else
            {
                m.head = a;
                m.tail = m.head;
            }
            a = a->next;
        }
        else if(a == NULL)
        {
            if(m.head != NULL)
            {               
                b->prev = m.tail;
                m.tail->next = b;
                m.tail = b;
            }
            else
            {
                m.head = b;
                m.tail = m.head;
            }
            b = b->next;
        }
        else if (a->data < b->data)
        {
            if(m.head == NULL)
            {
                m.head = a;
                m.tail = m.head;
            }
            else
            {
                a->prev = m.tail;
                m.tail->next = a;
                m.tail = a;
            }
            a = a->next;
        }
        else
        {
            if(m.head == NULL)
            {
                m.head = b;
                m.tail = m.head;
            }
            else
            {
                b->prev = m.tail;
                m.tail->next = b;
                m.tail = b;
            }
            b = b->next;
        }
    }
    return m.head;
}

node* mergeSort(node * a)
{
    if (head == NULL || head->next == NULL)
    {
        return a;
    }
    else
    {
        node * b = split();

        node* right = mergeSort(a);
        node* left = mergeSort(b);

        return merge(right, left);
    }
}



public:
    linkedList()
    {
        head = NULL;
        tail = NULL;
    }


void push_back(int x)
{
    node * baby = new node(x);

    if( head == NULL )
    {
        head=baby;
        tail=baby;
    }
    else
    {
        baby->prev = tail;
        tail->next = baby;
        tail = baby;
    }
}

void mergeSort()
{
    head = mergeSort(head);
}

bool empty()
{
    if (head == NULL)
        return true;
    else
        return false;
}

int pop()
{
    int popMe = head->data;
    node * deleteMe = head;
    if (head->next == NULL)
    {
        head = NULL;
        tail = NULL;
        delete deleteMe;
        return popMe;
    }
    else
    {
        head = head->next;
        head->prev = NULL;
        delete deleteMe;
        return popMe;
    }
}
//test
void display()
{
    node * finger = head;
    while(finger!=NULL)
    {
        cout << finger->data << endl;
        finger = finger->next;
    }
}

};

2 个答案:

答案 0 :(得分:0)

要进行合并排序,您必须拆分列表。例如,总是在头部和尾部分裂是没有意义的。它总是在整个列表上分裂,而不是列表中越来越小的部分。所以你的分裂必须看起来像这样:

void split(node *& a, node *& b)
{
    node * finger = a;
    node * fast = a->next;
    while (fast != NULL)
    {
        fast = fast->next;
        if (fast != NULL)
        {
            fast = fast->next;
            finger = finger->next;
        }
    }
    b = finger->next;
    b->prev->next = NULL;
    b->prev = NULL; 
}

我建议将变量名称fastfinger更改为更具描述性的名称。

Merge和mergeSort将需要类似的修改。

编辑帖子用C ++方式做事。对不起。

答案 1 :(得分:-1)

停止使用全局变量!
删除在函数之前出现的所有变量声明 仅依赖于作为参数传递给函数的数据。

修改

这是原始代码的(可能是最小的)修改,它不使用extern变量(无参数mergeSort()除外,它应该是唯一的公共方法,如果你在某些{{}中实现代码1}} class)。
该代码也不使用sortableList指针或last链接,因为它们在prev中不需要,并且可以在排序后轻松重建(在O(n)时间内很容易在这里'在排序列表中单次传递。)

mergeSort

编辑2

您的// split the list and return a pointer to a second half node * split(node * fast) { // assert(fast != NULL) node * finger = fast; fast = fast->next; while (fast != NULL) { fast = fast->next; if (fast != NULL) { fast = fast->next; finger = finger->next; } } node * splitB = finger->next; finger->next = NULL; return splitB; } // sort the (sub)list and return its new head node* mergeSort(node * a) { // assert(a != NULL) // list not empty node * b = split(a); // take second half if (b != NULL) { a = mergeSort(a); b = mergeSort(b); a = merge(a, b); } return a; } void mergeSort() { if (head != NULL) // make sure list isn't empty head = mergeSort(head); } 例程似乎太长且过于复杂。请参阅下面的简短版本。它不是更容易变红吗?

merge

如果你创建一个临时的node * merge(node * a, node * b) { // assert(a != NULL) // assert(b != NULL) node * head, * tail; if (a->data <= b->data) head = a, a = a->next; else head = b, b = b->next; tail = head; while (a != NULL && b != NULL) { if (a->data <= b->data) tail->next = a, a = a->next; else tail->next = b, b = b->next; tail = tail->next; } if (a != NULL) tail->next = a; else tail->next = b; return head; } 变量,你也可以摆脱第一个if(但有时候不允许这样做,比如说node类太复杂或者它的构造会引起一些不必要的副作用):

node

这是在前向链接列表排序后恢复node * merge(node * a, node * b) { // assert(a != NULL) // assert(b != NULL) node head, * tail = &head; while (a != NULL && b != NULL) { if (a->data <= b->data) tail = tail->next = a, a = a->next; else tail = tail->next = b, b = b->next; } tail->next = a ? a : b; return head->next; } 链接的方法:

prev