2017-03-15 51 views
-2
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()); 

     } 

    } 

虽然我的气泡排序算法工作得很好,但它们只是每次单击排序按钮时都会逐渐排序。我需要使用1次点击完全排序列表框。使用结构成员时的气泡排序

enter image description here

而且,我的代码现在,岁月和销售重组完全相互独立的。当销售指数变化时,相应的年份指数保持在相同的位置,反之亦然。

我猜一个与INT j循环会的工作,但我不知道如何实现它。任何帮助,将不胜感激!

回答

0

我猜你会因为学习/练习的原因而进行冒泡排序。如果不是,你应该只使用内置的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#代码。

1

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

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

这导致第二个问题。你有一个使用索引变量i和j的双重循环。你的交换只使用我。如果你正在尝试做一个冒泡排序,你真的需要嵌套循环吗?考虑在这里可以找到的伪代码实现bubble sort,你应该很快就能看到问题。在该示例之后建模您的排序。

+0

是有道理的,虽然事实是我使用的结构作为一个阵列(SSales []气泡=新SSales [10]),所以我如何分配变种温度? – user7115764

+0

您可以像我在示例中那样分配临时变量。基本上,你正在分配整个结构。通过使用'var'关键字,您不必担心类型,编译器将使用适当的类型。 – Dweeberly