【问题标题】:bubble sort and foreach index冒泡排序和foreach索引
【发布时间】:2013-02-21 00:31:17
【问题描述】:

我在下面有一些用于按钮单击事件的代码, 它使用冒泡排序。我有点不清楚 的使用。尝试按升序对数组进行排序 命令。我也必须使用 foreach 并且需要 以某种方式从中获取索引。 尝试 int z = a.GetEnumerator();不起作用。 int k = 0;//作弊让代码工作

        int k = 0;//Cheat to get code working
        foreach (BankAccount BankAccount in a)
        //for (int i = 0; i < a.Length; i++)
        {
            //int z = a.GetEnumerator();
            lstBefore.Items.Add(a[k].show());
            k += 1;//Cheat to get code working
        }
        //if (a[0] is IComparable)
        //{
            //Sort.BubbleSort(a);//Sort a
            k = 0;//Cheat to get code working
            for (int i = 0; i < a.Length; i++)
            {
                lstAfter.Items.Add(a[k].show());
                //else MessageBox.Show("unable to sort");
                k += 1;//Cheat to get code working
            }
        //}
        //else MessageBox.Show("unable to sort");

class Sort : IComparable
{
    public static void BubbleSort(IComparable[] arr)
    {
        bool swap = true;
        IComparable temp;
        for (int i = 0; swap; i++)
        {
            swap = false;
            for (int j = 0; j < (arr.Length - (i + 1)); j++)
            {
                //int test = arr[j].CompareTo(arr[j + 1]);                    
                if (arr[j].CompareTo(arr[j + 1]) > 0)
                //If this balance is < than next balance                    
                {
                    temp = (IComparable)arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                    swap = true;
                }
            }
        }
    }
}

我也有

public class BankAccount : IComparable, IComparable<BankAccount>//Class BackAccount -             //Icomarable    
{
    private decimal balance;
    private string FullName;
    //...

    public int CompareTo(BankAccount that)//Compare To        
    {
        if (this.balance > that.balance) return -1;//If this balance is > than next balance            
        if (this.balance == that.balance) return 0;//If this balance is = to next balance            
        return 1;//If this balance is < than next balance            
        //return this.balance.CompareTo(that.balance);        
    }
}
Thanks,

【问题讨论】:

  • 是的,这样更好
  • 您的CompareTo 倒退了。如果this.balance &gt; that.balance,它应该返回1。除非你想让它倒序排列。

标签: c#


【解决方案1】:

看起来您只需要在 Sort 类中实现 CompareTo(而不仅仅是 BankAccount 类)。

【讨论】:

  • 你的意思是给他的Sort类添加一个CompareTo方法吗?
【解决方案2】:

首先,foreach 没有任何索引。如果需要索引,请使用 for 循环。
其次,Sort 类不必实现IComparable(这会导致错误)。这是一个比较器,而不是比较。如果你愿意,它可以实现IComparer,或者是静态的。
当你有 Array.SortList.Sort 方法时,你为什么要潜入冒泡排序实现,这些方法实现了 QuickSort 并且肯定更快更好?我会避免这种情况。

【讨论】:

  • 也许他正在学习排序算法并希望看到冒泡排序的实际应用。
  • 是的,每个都一样。我必须编写一个名为 ShowAccounts 的方法,该方法使用 foreach 构造来显示数组中的每个元素。并使用带有 IComparable 的 BubbleSort 按余额金额升序对帐户进行排序。
  • 我有例如 new BankAccount("Micky Mouse", 900m) 并且必须对余额进行排序。
  • 保持索引变量并推进它,就像您所做的那样,是在 foreach 循环中拥有索引的正常方法。至于IComparable,他们的意思是BankAccount 需要是IComparable 才能进行排序,而不是Sort 类,它不是“可比较的”。
猜你喜欢
  • 2017-03-11
  • 1970-01-01
  • 1970-01-01
  • 2015-02-14
  • 2013-10-09
  • 1970-01-01
  • 2015-09-12
相关资源
最近更新 更多