单链表上的归并排序 C++

发布于 2024-11-04 06:36:40 字数 860 浏览 5 评论 0原文

我正在寻找某种简单的方法来学习和理解这些合并排序。我在网上查看过,发现合并排序对于单链表确实很好,但我不明白如何做到这一点。这是我找到的网站: 维基百科合并排序具体链接列表

我不确定要给你什么代码。我基本上只是在我的头文件中添加了这个,并且对此很陌生,所以我非常基础。提前感谢您的帮助:)

class Node
{
public:
    int data;
    Node* next;
    Node()
    {
        next = NULL;
        data = 0;
    }
};

class SLLIntStorage
{
public:
    Node* head;
    Node* current;
    Node* tail;

    void Read(istream&);
    void Write(ostream&);
    void setReadSort(bool);
    void sortOwn();
    void print();

    bool _sortRead;
    int numberOfInts;

    SLLIntStorage(const SLLIntStorage& copying)
    {

    }

    SLLIntStorage(void);
    ~SLLIntStorage(void);
};

I am looking for some kind of simple way I can learn and understand merge sort on these. I have looked on the web and it has been found that merge sort is really good for singly linked lists but I cant understand how to do it. This is the websites I found:
Wikipedia Merge sort and
Specifically linked lists

I am not sure what code to give you. I basically just have this in my header file and am new to this so I am very basic. Thank you for your help in advance :)

class Node
{
public:
    int data;
    Node* next;
    Node()
    {
        next = NULL;
        data = 0;
    }
};

class SLLIntStorage
{
public:
    Node* head;
    Node* current;
    Node* tail;

    void Read(istream&);
    void Write(ostream&);
    void setReadSort(bool);
    void sortOwn();
    void print();

    bool _sortRead;
    int numberOfInts;

    SLLIntStorage(const SLLIntStorage& copying)
    {

    }

    SLLIntStorage(void);
    ~SLLIntStorage(void);
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

高跟鞋的旋律 2024-11-11 06:36:40

如果你从维基百科上看这一段

从概念上讲,合并排序的工作方式如下
如下

  1. 如果列表的长度为 0 或 1,则它已经排序。否则:
  2. 将未排序列表分成两个大小约为一半的子列表。
  3. 通过重新应用合并排序对每个子列表进行递归排序。
  4. 将两个子列表合并回一个已排序的列表。

这非常简洁地告诉您需要做什么以及需要什么样的操作。第 2 句和第 4 句是您需要能够执行 Split()Merge() 的操作。 Split 可以在您的 Node 类上实现,

// Split: removes half of the list and returns a pointer to it
Node* Node::Split()
{
} 

类似地 Merge 可以作为一个整体来实现

// Merge: insert elements from source in order
Node::Merge(Node* source)
{
}

1,2,3,4 描述执行实际排序所需执行的操作 按顺序执行这些步骤通过使用列表操作 Merge 和 Split 来实现排序功能。

这只是MergeSplit 的一种方式,如何实现它们将取决于您的风格、要求、C++ 知识以及各种其他因素。 (我相信您会在答案中看到其他一些解决方案)。您可能还需要一个 Node::Append(Node* val) 或类似的基本运算符。看起来您不需要 Node::Remove(Node* val) ,但实现它也可能不会有什么坏处。

If you look at this paragraph from wikipedia

Conceptually, a merge sort works as
follows

  1. If the list is of length 0 or 1, then it is already sorted. Otherwise:
  2. Divide the unsorted list into two sublists of about half the size.
  3. Sort each sublist recursively by re-applying the merge sort.
  4. Merge the two sublists back into one sorted list.

This tells you pretty succinctly what you need to do and what kind of operations you need. sentences 2 and 4 are operations that you need to be able to do Split() and Merge(). Split could be implemented on your Node class as

// Split: removes half of the list and returns a pointer to it
Node* Node::Split()
{
} 

similarly Merge could be implemented as

// Merge: insert elements from source in order
Node::Merge(Node* source)
{
}

as a whole 1,2,3,4 describe what you need to do to perform the actual sorting implement these steps in order in the sorting function by using the list operations Merge and Split.

This is only one way Merge and Split could look like, how you implement them will depend on your style, requirements, knowledge of c++ and various other factors. (I am sure you will see some other solutions in the answers). You will probably also need a Node::Append(Node* val) or something similar as a basic operator. It does not look like you will need Node::Remove(Node* val) but it might not hurt to implement that too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文