简体   繁体   English

从C到C#的递归MergeSort转换

[英]Recursive MergeSort translation from C to C#

I am completely stuck with this recursive implementation of a merge sort due to the .NET requirement that all variables must first be instantiated before being used. 由于.NET要求所有变量在使用之前必须首先实例化,因此我完全陷入了合并排序的递归实现。 My professor wants us to translate C code for MergeSort into C#, using a recursive algorithm. 我的教授希望我们使用递归算法将MergeSort的C代码转换为C#。 Here is the C code: 这是C代码:

void doMergeSort(contact **head)
{
    // do not process empty or single-node list
    if (head==NULL || *head == NULL || (*head)->next==NULL)
        return;

    contact *first, *second;

    // split the list in half
    splitList(*head, &first, &second);

    // perform recursive merge sort on both halves
    doMergeSort(&first);
    doMergeSort(&second);

    // Merge both halves back together,
    mergeList(first, second)
}

Here is my implementation of it: 这是我的实现:

public static void DoMergeSort(ref LinkNode<T> head)
{
    if (head == null || head.Next == null)
        return;

    LinkNode<T> first = null, second = null;

    SplitList(head, ref first, ref second);

    DoMergeSort(ref first);
    DoMergeSort(ref second);

    head = MergeLists(first, second);
}

Now, because of the rules of C#/.NET, I cannot leave out instantiating first and second, but declaring them as null within the function screws up the recursive nature of it. 现在,由于C#/。NET的规则,我不能遗漏首先实例化第二个实例,而是在函数中将它们声明为null会破坏它的递归性质。 I have been stuck on this problem for multiple days and and my professor is being little help. 我已经在这个问题上停留了好几天,而我的教授却无济于事。 Would I be better off trying to translate this into an iterative function, or is there a way I can accomplish this recursively? 我会尝试将其转换为迭代函数,还是有一种方法可以递归完成呢?

My only concern with taking an iterative approach is that it would "break the rules" of the assignment. 我唯一关心的是采用迭代方法,这会“破坏规则”。

I may be missing something about C#, but setting first and second = null should not be a problem since SplitList(...) is supposed to split the list from head (which is not null) into two halves, the first half goes into left, the second half goes into second. 我可能缺少有关C#的信息,但设置first和second = null应该不是问题,因为SplitList(...)应该将列表从头(不为null)拆分成两半,上半部分变成左,下半部分进入第二。 Then the recursion occurs with the now non-empty first and then second lists, and each level of recursion creates yet another pair of first and second. 然后使用现在非空的第一和第二列表进行递归,每个递归级别都创建另外一对第一和第二对。

The code doesn't quite follow the C code, you need to check if a list only has one node (the C code checks (*head)->next == NULL to do this). 代码并不完全遵循C代码,您需要检查列表中是否只有一个节点(C代码检查(* head)-> next == NULL来执行此操作)。 If there's only one node, the function should just return. 如果只有一个节点,则该函数应返回。

Although not a part of your class assignment, there is an iterative version of merge sort for linked lists. 尽管这不是类分配的一部分,但是对于链接列表,它有一个合并合并的迭代版本。 It uses a small (26 to 32) array of internal lists. 它使用一小列(26至32个)内部列表。 It's faster than the recursive version since it doesn't have to scan sub-lists to split them. 它比递归版本要快,因为它不需要扫描子列表来拆分它们。 Wiki article describes this. Wiki文章对此进行了描述。

https://en.wikipedia.org/wiki/Merge_sort#Bottom-up_implementation_using_lists https://zh.wikipedia.org/wiki/Merge_sort#Bottom-up_implementation_using_lists

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM