C#排序列表<;int>;递归地

C#排序列表<;int>;递归地,c#,list,recursion,C#,List,Recursion,我需要做一个练习,给定一个列表,我只需要使用递归方法(no-while、do-while、for、foreach)对内容进行排序 所以。。。我在挣扎(现在已经两个多小时了),我甚至不知道如何开始。 函数必须是 List<int> SortHighestToLowest (List<int> list) { } List SortHighestToLowest(列表列表){ } 我想我应该检查一下前一个数字是否大于实际数字等等,但是如果最后一个数字大于列表上的第一个数

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

所以。。。我在挣扎(现在已经两个多小时了),我甚至不知道如何开始。 函数必须是

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

}
List SortHighestToLowest(列表列表){
}
我想我应该检查一下前一个数字是否大于实际数字等等,但是如果最后一个数字大于列表上的第一个数字呢?这就是我头痛的原因

我感谢你的帮助,非常感谢

[编辑]

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

    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);
    }
List_tempList2=newlist();
int _actualistindex=0;
int _实际MaxNumber=0;
int_实际MaxNumberIndex=0;
列表SortHighestToLowest(列表列表)
{
如果(list.Count==0)
返回模板2;
如果(_ActualistIndex==0)
_actualMaxNumber=列表[0];
如果(_actualistindex\u actualMaxNumber)
{
_actualMaxNumberIndex=_actualListIndex;
_actualMaxNumber=列表[_ActualistIndex];
}
返回SORTHIGHESTOLOWEST(列表);
}
_模板2.添加(_实际MaxNumber);
列表.删除(_actualMaxNumberIndex);
_实际指数=0;
_实际MaxNumberIndex=0;
返回SORTHIGHESTOLOWEST(列表);
}

练习已经完成,我也同意了(感谢其他练习),但我想知道是否有一种方法可以在不使用外部变量和系统的情况下完成。Linq like的回答(我只是好奇,社区帮助我解决了我的问题,我很感谢)。

要解决这个问题,尝试解决较小的子集。考虑下面的列表

[1,5,3,2]

让我们从列表中取出最后一个元素,并将其余的元素排序为“<代码> [1,3.5] 和<代码> 2 。现在问题归结为另一个问题,即在正确的位置插入此

2
。如果我们可以将其插入正确的位置,那么数组将被排序。这可以递归应用。
对于每个递归问题,我们所做的假设都应该有一个基本条件。对于第一个问题,基本条件是具有单个元素的数组。单个元素数组总是被排序的。
对于第二个
insert
问题,基本条件是空数组,或者数组中的最后一个元素
小于要插入的元素。在这两种情况下,图元都是在末端插入的

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)
算法
---------
排序(列表)
if(list.count==1)
返回
temp=列表的最后一个元素
临时列表=删除最后一个元素的列表
排序(临时列表)
插入(临时列表,临时)
插入(列表,临时)

如果(list.count==0 | | | list[n-1]

  • 只有递归方法
  • 没有时间,做时间,为了,为了
  • 签名必须是列表或最下面(列表)
  • 现在,我假设您至少可以使用列表类型的内置属性和方法,否则,您甚至很难读取列表中的元素

    也就是说,对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.Count)这能回答您的问题吗?@devNull否,但这只是一个开始,我将使用它作为“模板”。谢谢!您可能希望对
    列表执行此操作,其中T:IComparable
    。它将是相同的逻辑(除非您将使用
    IComparable
    方法,而不是
    或code>
    )。由于整数是可比较的,它们将只是由于感知到的“焦点”而请求关闭的工作人员可能应该注意到这正是我所寻找的,现在我有一个问题,有没有一种不使用list.Max()的方法来实现这一点?(只是好奇).哦,你知道,我用我曾经批准过的代码编辑了答案。@DEFALTUSER嘿,很高兴能帮上忙。我检查了你的编辑,做得好!是的,我知道有些人可能不赞成使用Max()。事实上,我也是。但是考虑到其他限制,如果没有Max()或Min(),很难做到这一点。大多数代码在使用循环和将其他变量作为参数传递给递归方法方面都有一定的灵活性,但本练习明确禁止这些。也就是说,我将再试一次,如果有任何进展,请更新此答案。干杯!您的代码在逻辑方面帮助了我,我刚刚编辑了我的答案以显示我在中所做的工作如果你好奇的话,谢谢!