【问题标题】:C# sort List<int> recursivelyC# 递归排序 List<int>
【发布时间】:2021-03-08 19:49:32
【问题描述】:

我需要做一个练习,给定一个列表,我只需要使用递归方法(不使用 while、do while、for、foreach)对内容进行排序。

所以...我正在苦苦挣扎(现在已经超过 2 小时),我什至不知道如何开始。 函数必须是

List<int> SortHighestToLowest (List<int> list) {

}

我想我应该检查前一个数字是否大于实际数字等等,但是如果最后一个数字大于列表中的第一个数字怎么办?这就是为什么我很头疼。

感谢您的帮助,非常感谢。

[编辑]

我完成了练习,但老师说我不应该像这里那样使用外部变量:

    List<int> _tempList2 = new List<int>();
    int _actualListIndex = 0;
    int _actualMaxNumber = 0;
    int _actualMaxNumberIndex = 0;

    List<int> SortHighestToLowest(List<int> list)
    {
        if (list.Count == 0)
            return _tempList2;

        if (_actualListIndex == 0)
            _actualMaxNumber = list[0];

        
        if (_actualListIndex < list.Count -1)
        {
            _actualListIndex++;

            if (list[_actualListIndex] > _actualMaxNumber)
            {
                _actualMaxNumberIndex = _actualListIndex;
                _actualMaxNumber = list[_actualListIndex];
            }

            return SortHighestToLowest(list);
        }

        _tempList2.Add(_actualMaxNumber);
        list.RemoveAt(_actualMaxNumberIndex);
        _actualListIndex = 0;
        _actualMaxNumberIndex = 0;
        return SortHighestToLowest(list);
    }

练习已经完成并且我批准了(也感谢其他练习)但我想知道是否有一种方法可以不使用 外部变量 并且不使用 System.Linq 喜欢String.Empty 的回复(我只是好奇,社区帮助我解决了我的问题,我很感激)。

【问题讨论】:

  • 这能回答你的问题吗? Bubble sort using recursion in C#
  • @devNull 不,但它是一个开始,我将使用它作为“模板”。谢谢!
  • 您可能想为List&lt;T&gt; where T:IComparable 这样做。这将是相同的逻辑(除了您将使用IComparable 方法而不是&lt;&gt; 之类的方法)。由于整数是可比较的,它们将正常工作
  • 因感知到“焦点”而请求关闭的人或许应该注意Is it always a good idea to demand the OP “post some code”?

标签: c# list recursion


【解决方案1】:

为了解决这个问题,尝试解决更小的子集。考虑以下列表

[1,5,3,2]

让我们从列表中取出最后一个元素,并将其余元素视为已排序,即[1,3,5]2。现在问题归结为将2 插入正确位置的另一个问题。如果我们可以将它插入正确的位置,那么数组就会排序。这可以递归应用。
对于每个递归问题,都应该有一个基本条件 w.r.t 我们做出的假设。对于第一个问题,基本条件是具有单个元素的数组。单个元素数组总是排序的。
对于第二个insert 问题,基本条件将是一个空数组或数组中的最后一个元素是less than 要插入的元素。在这两种情况下,元素都插入到末尾。

Algorithm
---------
Sort(list)
   if(list.count==1)
       return
   temp = last element of list
   temp_list = list with last element removed
   Sort(temp_list)
   Insert(temp_list, temp)

Insert(list, temp)
   if(list.count ==0 || list[n-1] <= temp)
      list.insert(temp)
      return
   insert_temp = last element of list
   insert_temp_list = list with last element removed

   Insert(insert_temo_list, insert_temp)

对于Insert,在基本条件之后递归调用,直到找到被移除的最后一个元素的正确位置。

【讨论】:

  • 您的代码在逻辑上帮助了我,我刚刚编辑了我的答案以显示我所做的事情,以防您好奇。谢谢!
【解决方案2】:

我在这里接受你的指示。

  1. 仅递归方法
  2. 不一会儿,一会儿做,一会儿做,foreach,foreach
  3. 签名必须是 List SortHighestToLowest(List list)

现在,我假设您至少可以使用 List 类型的内置属性和方法。否则,您甚至很难阅读列表中的元素。

也就是说,对 SortOrderBy 方法的任何调用都超出了这里的重点,因为它们会使任何递归方法变得无用。

我还认为在此过程中使用其他列表是可以的,因为您没有提及任何相关内容。

考虑到所有这些,我来到下面的这篇文章,使用 List 类中的 MaxRemove 方法,以及一个新的整数列表对于每个递归调用:

    public static List<int> SortHighestToLowest(List<int> list)
    {
        // recursivity breaker
        if (list.Count <= 1)
            return list;

        // remove highest item
        var max = list.Max();
        list.Remove(max);

        // append highest item to recursive call for the remainder of the list
        return new List<int>(SortHighestToLowest(list)) { max };
    }

【讨论】:

  • 这正是我一直在寻找的,现在我有一个问题,有没有办法做到这一点使用 list.Max()? (只是好奇)。哦,你知道,我用我过去批准的代码编辑了答案。
  • @DEFALTUSER 你好。乐意效劳。我检查了你的编辑,做得好!是的,我知道有些人可能不赞成 Max() 的使用。其实我也是,呵呵。但是考虑到其他限制,如果没有 Max() 或 Min(),将很难实现这一点。大多数代码在使用循环和将其他变量作为参数传递给递归方法方面具有一定的灵活性,但本练习明确禁止这些。也就是说,如果我得到任何地方,我会再试一次并更新这个答案。干杯!
猜你喜欢
  • 1970-01-01
  • 2015-01-14
  • 1970-01-01
  • 1970-01-01
  • 2011-06-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多