【问题标题】:Bubble sort when using struct members使用结构成员时的冒泡排序
【发布时间】:2017-03-15 17:07:50
【问题描述】:
struct SSales
    {
        private int Y;
        private double S;

        public int Year
        {
            get { return Y; } 
            set { Y = value; }

        }
        public double Sale
        {
            get { return S; }
            set { S = value; }
        }

        public SSales (int _year, double _sales)
        {

            Y = _year;
            S = _sales;

        }

private void Sortbutton_Click(object sender, EventArgs e)
    {
        listBox1.Items.Clear();
        if (yearradio.Checked)
        {
            int temp = 0;
            for (int i = 0; i < bubble.Length - 1; i++)
            {
                for (int j = 0; j < bubble.Length - 1; j++)
                {
                    if (bubble[i + 1].Year < bubble[i].Year)
                    {
                        temp = bubble[i].Year;
                        bubble[i].Year = bubble[i + 1].Year;
                        bubble[i + 1].Year = temp;
                    }
                }
            }

        }
        if (salesradio.Checked)
        {
            double temp2 = 0;
            for (int i = 0; i < bubble.Length - 1; i++)
            {
                for (int j = 0; j < bubble.Length - 1; j++)
                {
                    if (bubble[i + 1].Sale > bubble[i].Sale)
                    {
                        temp2 = bubble[i].Sale;
                        bubble[i].Sale = bubble[i + 1].Sale;
                        bubble[i + 1].Sale = temp2;

                    }
                }
            }
        }
        for (int i = 0; i < bubble.Length; i++)
        {

            listBox1.Items.Add(bubble[i].ToString());

        }

    }

虽然我的冒泡排序算法工作得非常好,但它们只会随着每次单击排序按钮而递增排序。我需要一键对列表框进行完全排序。

另外,就像我现在的代码一样,Years 和 Sales 完全相互独立地重新组织。当销售指数发生变化时,相应的年份指数保持在同一位置,反之亦然。

我猜测带有 int j 的 for 循环会起作用,但我不确定如何实现它。任何帮助将不胜感激!

【问题讨论】:

    标签: c# sorting struct bubble-sort


    【解决方案1】:

    我看到两个问题。您正在设置/交换结构的属性,而不是结构本身。这就是为什么您的销售额和年份不同步的原因。您需要交换整个结构。比如:

                        var temp = bubble[i];
                        bubble[i] = bubble[i + 1];
                        bubble[i + 1] = temp;
    

    这就引出了第二个问题。您有一个使用索引变量 i 和 j 的双循环。您的交换仅使用 i。如果您尝试进行冒泡排序,您真的需要嵌套循环吗?考虑可以在此处找到的伪代码实现bubble sort,您应该很快就能看到问题所在。仿照该示例为您的排序建模。

    【讨论】:

    • 这是有道理的,但问题是我将结构用作数组(SSales[] bubble = new SSales[10]),那么我该如何分配 var temp?
    • 您可以像我在示例中所做的那样分配临时变量。基本上,您正在分配整个结构。通过使用 'var' 关键字,您不必担心类型,编译器将使用适当的类型。
    【解决方案2】:

    我猜你是出于学习/实践的原因而进行冒泡排序。如果不是,您应该只使用内置的 Array.Sort()Enumerable.OrderBy() 或类似的东西。

    你做错了很多事情。我有改进的代码,我将在下面解释

    struct SSales {
        public int Year { get; set; } // use auto-properties for brevity
    
        public double Sale { get; set; }  // use auto-properties for brevity
    
        public SSales(int year, double sales) {
            Year = year;
            Sale = sales;
        }
    }
    
    // Use a generic routine to Swap, instead of replicating the code multiple times
    // Note that we are passing by reference so the actual array eventually gets sorted
    // Also, don't swap the properties, but the whole record. Else it will corrupt your data
    static void Swap<T>(ref T obj1, ref T obj2) {
        var temp = obj1;
        obj1 = obj2;
        obj2 = temp;
    }
    
    // Write the sort routine separately. Sorts usually just need a way to compare records, which can be provided by Caller (IoC pattern)
    static void Sort<T>(T[] items, Func<T, T, int> comparer) {
        for (int i = 0; i < items.Length - 1; i++) {
            // Every execution of the inner loop will bubble-up the largest element in the range
            // Your array is getting sorted from the end, so you don't need to re-compare the already sorted part
            for (int j = 0; j < items.Length - 1 - i; j++) {
                if (comparer(items[j], items[j + 1]) > 0) // call the generic user provided comparer to know the sequence
                    Swap(ref items[j], ref items[j + 1]); // use teh generic swapper to swap elements in the array
            }
        }
    }
    
    
    private void Sortbutton_Click(object sender, EventArgs e) {
        listBox1.Items.Clear();
    
        if (yearradio.Checked) {
            // Invoke the Sort routine, with the required comparer
            Sort(bubble, (a, b) => a.Year - b.Year);
        }
    
        if (salesradio.Checked) {
            // Invoke the Sort routine, with the required comparer
            Sort(bubble, (a, b) => (int)(a.Sale - b.Sale));
        }
    
        for (int i = 0; i < bubble.Length; i++) {
            listBox1.Items.Add(bubble[i].ToString());
        }
    
    }
    

    希望能够阐明您面临的问题,并帮助您学习如何编写更好的 C# 代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-09-12
      • 1970-01-01
      • 1970-01-01
      • 2020-07-17
      • 2020-07-11
      • 2015-06-21
      • 1970-01-01
      相关资源
      最近更新 更多