2017-03-15 53 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