【问题标题】:Insertion Sorting c#插入排序 c#
【发布时间】:2013-02-06 09:37:07
【问题描述】:

你们能帮我在 C# 中进行基本的插入排序吗?我有一个数组中的姓名和居住城市列表,需要通过比较居住城市来对该数组进行排序。列表必须按字母顺序排序。比较器已经设置并且可以工作了我只是有点迷失了插入排序器编程,因为这是我们第一次使用这种排序方法。

这是我迄今为止尝试过的:

public void InsertionSort()
{
    for (int i = 0; i < Count; i++)
    {
        Student cur = Attendees[i];
        for (int j = 0; j < Count; j++)
        {
            Student Sel = Attendees[j];
            if (cur.CompareTo(Sel) < 0)
            {
                Student temp = Attendees[j];
                Attendees[j] = Attendees[i];
                for (int k = i; k > j; k--)
                    Attendees[k] = Attendees[k - 1];
                Attendees[k + 1] = temp;
            }
        }
    }
}

【问题讨论】:

  • 步骤: 1- 编辑您的问题。 2-发布一些尝试/代码。结果:获得更少的反对票 + 获得答案/建议。
  • 谷歌是你的朋友!这是一张照片:csharp-examples.net/sort-array
  • public void InsertionSort() { for (int i = 0; i j; k--) 参加者[k] = 参加者[k - 1];参加者[k + 1] = temp;
  • 只是在寻找关于开始的提示,或者我是否在正确的轨道上
  • 上面的 cmets 对新手非常没有帮助 - 只需回答他的问题,他给了你一个例子。呼!!

标签: c# insertion-sort


【解决方案1】:

试试这样...

public void InsertionSort()
{
    for (int i = 0; i < Count; i++)
    {
        int j = i;
        While(j > 0)
        {
            Student cur = Attendees[j];
            Student sel = Attendees[j-1];
            if (cur.CompareTo(Sel) < 0)
            {
                Student temp = cur;
                cur = sel;
                sel = temp;
                j--
            }
            else
                break;
        }
    }
}

【讨论】:

    【解决方案2】:
    public void InsertionSort()
    {
        for (int i = 1; i < Count; i++) // Iterate beginning at 1, because we assume that 0 is already sorted
        {
            for (int j = i; j > 0; j--) // Iterate backwards, starting from 'i'
            {
                Student cur = Attendees[j - 1];
                Student tbs = Attendees[j]; // 'tbs' == "to be sorted"
                if (cur.CompareTo(tbs) < 0) // usually, classes that implement 'CompareTo()' also implement 'operator <()', 'operator >()' and 'operator ==()', so you could have just written 'cur < tbs'
                {
                    Student temp = Attendees[j];
                    Attendees[j] = Attendees[j - 1];
                    Attendees[j - 1] = temp;
                }
                else
                    break; // since 'tbs' is no longer > 'cur', it is part of our sorted list. We don't need to sort that particular 'tbs' any further
            }
        }
    }
    

    请记住,此算法按降序对您的列表进行排序。

    【讨论】:

      【解决方案3】:
      int[] newarr = {2,1,5,3,7,6};
      int a, b;
      for (int i = 1; i < newarr.Length; i++)
      {
          a = newarr[i];
          b = i - 1;
          while(b>=0 && newarr[b]>a)
          {
              newarr[b+1] = newarr[b];
              b=b-1;
          }
          newarr[b+1] = a;
      }
      

      【讨论】:

      • 虽然这段代码可能会解决问题,但一个好的答案还应该解释代码的什么以及它如何提供帮助。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-08-26
      • 1970-01-01
      • 1970-01-01
      • 2019-04-03
      • 2013-12-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多