【问题标题】:Logic of a sort-algorithm [closed]排序算法的逻辑[关闭]
【发布时间】:2013-09-21 19:53:57
【问题描述】:

我想弄明白这个排序算法的逻辑,但是实在弄不明白,可能是我不够聪明……

我有一个清单。此列表已排序,假设我有 10 个项目。

现在用户选择将第 9 个项目放在第 4 个位置。

怎么办?

我的想法:

  • 将 9. 物品放入临时对象中。
  • 将 8 项放置到位 9
  • 将 7 项放置到位 8
  • 将 6 项放置到位 7
  • 将 5 项放置到位 6
  • 将 4 项放置到位 5
  • 将 9 件物品放置到位 4

这个想法对吗?

【问题讨论】:

  • 这并不是真正的“排序”算法,而更像是“删除”和“插入”算法的结合
  • 做什么?使用SortedList收藏。
  • 是的,我同意 Scott :)
  • 看起来像List.Insert...不清楚您要实现的目标...

标签: c# asp.net sorting


【解决方案1】:

您似乎在谈论轮换。这是一篇包含几个想法的帖子,并对各种方法的优缺点进行了体面的讨论:

Easiest way to Rotate a List in c#

【讨论】:

    【解决方案2】:

    执行所有这些单独的移动将是低效的,更好的方法是使用单个 Array.Copy 方法来处理对象的跨度。

    我将使用数组而不是列表,就像您使用 List<T> 一样,您可以使用 RemoveAt 和 Insert

    public static void MoveIndex<T>(this T[] array, int sourceIndex, int destIndex)
    {
        //Some sanity checks before we start.
        var arrayLength = array.Length;
        if(sourceIndex >= arrayLength || destIndex >= arrayLength || sourceIndex < 0 || destIndex < 0)
            throw new IndexOutOfRangeException("The indexes must be within the array);
    
        if(sourceIndex == destIndex)
            throw new ArgumentException("The two indexes must not have the same value");
    
        //Store for later useage
        var temp = array[sourceIndex];
    
        int offset;
        int length;
    
        //Figure out if we are moving left or right
        if(souceIndex < destIndex)
        {
            offset = -1;
            length = destIndex - sourceIndex;
        }
        else
        {
            offset = 1;
            length = sourceIndex - destIndex;
        }
    
        //"Move" the elements that need shifting
        Array.Copy(array, sourceIndex, array, sourceIndex + offset, length);
    
        //put back the item that we stored earlier;
        array[destIndex] = temp;
    
    }
    

    如果您可以将您的收藏设置为 ObservableCollection&lt;T&gt;,那么它的 Move 函数中就内置了它

    【讨论】:

      【解决方案3】:

      这是一个使用 SortedList 的示例。您也可以只创建一个列表并在其上调用 Sort()。

      using System;
      using System.Collections;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      using System.Threading.Tasks;
      
      namespace sorter
      {
          class Program
          {
              static void Main(string[] args)
              {
                  var list = new SortedList();
                  var item = new SomeItem(1);
                  list.Add(item.Value, item);
                  item = new SomeItem(8);
                  list.Add(item.Value, item);
                  item = new SomeItem(2);
                  list.Add(item.Value, item);
                  item = new SomeItem(4);
                  list.Add(item.Value, item);
      
                  for (int i = 0; i < list.Count; i++)
                  {
                      Console.WriteLine(list.GetByIndex(i));
                  }
      
                  Console.ReadLine();
              }
          }
      
          public class SomeItem
          {
              public int Value;
      
              public SomeItem(int value)
              {
                  Value = value;
              }
      
              public override string ToString()
              {
                  return Value.ToString();
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-04-14
        • 1970-01-01
        • 1970-01-01
        • 2014-03-18
        相关资源
        最近更新 更多