【问题标题】:Bubble sort using recursion in C#在 C# 中使用递归进行冒泡排序
【发布时间】:2009-10-29 15:14:01
【问题描述】:

我编写了这段简单的代码。而且我有一个小问题。

int [] x = [50,70,10,12,129];
sort(x, 0,1);
sort(x, 1,2);
sort(x, 2,3);
sort(x, 3,4);

for(int i = 0; i < 5; i++) 
 Console.WriteLine(x[i]);

static int [] sort(int [] x, int i, int j)
{
   if(j ==x.length) 
      return x;
   else if(x[i]>x[j])
   {
      int temp = x[i];
      x[i] = x[j];
      x[j] = temp;
      return sort(x, i, j+1);
    }
    else 
       return sort(x, i, j+1);
}

我觉得调用 sort 4 time 并不是最好的选择。我还需要一种使用 sort() 来处理这个问题的方法。我还向您征求意见、建议或提示。 谢谢

【问题讨论】:

  • 您是否有理由使用自己的 sort() 方法而不是使用 Array.Sort() 或 List.Sort()?

标签: c# recursion


【解决方案1】:

首先,您的排序仅限于整数,但是您可以使用IComparable&lt;T&gt; 接口将其扩展到任何可比较的类型。或者,您可以为Comparer&lt;T&gt; 设置另一个参数,以允许用户定义如何比较输入中的项目。

递归冒泡排序可能看起来像这样:(注意:未测试...)

public static T[] BubbleSort(T[] input) where T : IComparable<T>
{
    return BubbleSort(input, 0, 0);
}

public static T[] BubbleSort(T[] input, int passStartIndex, int currentIndex) where T : IComparable<T>
{
    if(passStartIndex == input.Length - 1) return input;
    if(currentIndex == input.Length - 1) return BubbleSort(input, passStartIndex+1, passStartIndex+1);

    //compare items at current index and current index + 1 and swap if required
    int nextIndex = currentIndex + 1;
    if(input[currentIndex].CompareTo(input[nextIndex]) > 0)
    {
        T temp = input[nextIndex];
        input[nextIndex] = input[currentIndex];
        input[currentIndex] = temp;
    }

    return BubbleSort(input, passStartIndex, currentIndex + 1);
}

但是,迭代解决方案可能会更有效且更易于理解...

【讨论】:

    【解决方案2】:

    简单的冒泡排序不需要递归。你可以这样做,只需传入数组进行排序:

    public int[] Sort(int[] sortArray)
        {
            for (int i = 0; i < sortArray.Length - 1; i++)
            {
                for (int j = sortArray.Length - 1; j > i; j--)
                {
                    if (sortArray[j] < sortArray[j - 1])
                    {
                        int x = sortArray[j];
                        sortArray[j] = sortArray[j - 1];
                        sortArray[j - 1] = x;
    
                    }
                }
            }
            return sortArray;
        } 
    

    【讨论】:

    • 嗯,“需要”在应用于递归算法时总是一个有趣的问题,如果这是自学练习,那么想要使用递归并不是不合理的。
    • 也许 Robban 应该说冒泡排序本身不借递归。使用专为递归设计的算法对递归排序进行编码可能更具教育意义。当然,这取决于 OP 想要学习什么。
    • 我可以在没有递归的情况下做到这一点。但是,我只是想看看如果我使用递归会发生什么。只是好奇!
    【解决方案3】:

    想要学习没有错 - 几件显而易见的事情。

    首先,您已经知道数组有一个长度属性 - 因此您可以使用它来创建一个循环,以摆脱在开始时对排序的多次调用,并使数组的长度成为问题。

    其次,您可能需要考虑排序的工作方式 - 怎么样:您正在尝试将值冒泡到列表中的正确位置(或者如果您愿意,可以向下!) - 所以对于列表n 项,删除第一项,对剩余的 n - 1 项进行排序(即递归位),然后将第一项冒泡到位。

    自从我想到这个已经几十年了,有趣!

    【讨论】:

      【解决方案4】:

      另一个只有 2 个参数:p 是的:

      static void Sort(IList<int> data)
      {
          Sort(data, 0);
      }
      
      static void Sort(IList<int> data, int startIndex)
      {
          if (startIndex >= data.Count) return;
      
          //find the index of the min value
          int minIndex = startIndex;
          for (int i = startIndex; i < data.Count; i++)
              if (data[i] < data[minIndex])
                  minIndex = i;
      
          //exchange the values
          if (minIndex != startIndex)
          {
              var temp = data[startIndex];
              data[startIndex] = data[minIndex];
              data[minIndex] = temp;
          }
      
          //recurring to the next
          Sort(data, startIndex + 1);
      }
      

      注意:这在现实生活中完全没用,因为 - 它非常慢 - 它的递归迭代是线性的,这意味着当你有超过 1k 个项目时,它会stackoverflow

      【讨论】:

        猜你喜欢
        • 2019-06-06
        • 2013-11-29
        • 1970-01-01
        • 2012-05-28
        • 1970-01-01
        • 2013-10-09
        • 2018-12-11
        • 1970-01-01
        • 2011-12-09
        相关资源
        最近更新 更多