2011-03-18 52 views
3

我正在C#中使用通用列表,但我嘗試使用冒泡排序方法對節點進行排序時遇到問題。在C#中的通用列表Bubblesort#

namespace ConsoleApplication1 
{  

public class GenericList 
{ 
    private class Node 
    { 
     // Each node has a reference to the next node in the list. 
     public Node Next;   
     public int Data; 
    } 

    // The list is initially empty. 
    private Node head = null; 

    // Add a node at the beginning of the list with t as its data value. 
    public void AddNode(int t) 
    { 
     Node newNode = new Node(); 
     newNode.Next = head; 
     newNode.Data = t; 
     head = newNode; 
    } 


//list length 
     public int Size() 
     { 
      int listsize= 0; 
      Node current = head; 
      while (current != null) 
      { 
       listsize++; 
       current = current.Next; 
      } 
      return listsize;    
     } 

     //bubble sort 
     public void bubblesort() 
     { 
      int size = Size(); 

      Node current = head; 

      for (int i = 1; i < size; i++) 
      { 
       for (int j = 0; j < size - 1; j++) 
       { 


        if (current.Data > current.Next.Data && current.Next!=null) 
        { 
         int temp = current.Data; 
         current.Data = current.Next.Data; 
         current.Next.Data = temp; 
        } 

       } 
      } 

      head = current; 
     } 
    } 
} 

當我向列表添加多於5個節點時,bubblesort方法停止工作(不正確地對列表進行排序)。 任何人都可以幫助嗎?

+3

這個功課?如果沒有,只需使用List.Sort方法。 – tvanfosson 2011-03-18 16:47:33

+2

確實看起來像一個大學班級的任務與這些意見...呃。 – 2011-03-18 16:49:22

+1

將其稱爲通用列表會使您聽起來像是在使用'System.Collections.Generic.List ',而不是您自己定製的'GenericList '。如果這不是家庭作業,只需要刪除'GenericList'並使用'List '(或'LinkedList '來更接近地匹配你所擁有的) – Justin 2011-03-18 16:50:39

回答

2

你需要澄清你的意思是「停止工作」......失敗?核心轉儲?沒有正確排序列表?

問題是你需要重置current回到head+1每次迭代ij迭代之前)。

如果要移動的最大值了,那麼j應該從1運行到size-i(因爲後先通過最大數量將在頂部 - 無需再檢查一遍)。或者將j下的最小值從size-1下調至i

嵌套循環方法的替代方法:您可以使用while/boolean/loop方法(外部while,boolean指示是否進行更改,循環內部)。當數據已經有點順序時,會有一些性能上的好處 - 它會在嵌套for方法之前停止處理(即使數據已經排序,它也會運行最大次數)。

0

你有兩個嵌套循環聲明ij變量,但你永遠不會在循環內使用它們。這顯然是錯誤的。

for (int i = 1; i < size; i++) 
{ 
    for (int j = 0; j < size - 1; j++) 
    { 

你應該做的是通過使用while循環,就像你在Size方法做:列表循環和交換相鄰的元素,如果他們是倒退。你會跟蹤bool你實際上是否進行過掉期,如果是,再次循環列表。這會重複,直到你沒有進行任何掉期。

這是假設你實際上需要實施冒泡排序來滿足你的任務需求。否則,沒有理由優先於框架中的內置集合類型和排序方法。

1

來吧夥計..削減他一些懈怠..這是谷歌的一代。

順便說一句..

http://www.google.co.uk/search?q=C%23+bubble+sort

..would給你一些很好的例子。

現在對於什麼是真正你的代碼錯誤:

此代碼(從上面)

Node current = head; 

    for (int i = 1; i < size; i++) 
    { 
     for (int j = 0; j < size - 1; j++) 
     { 


      if (current.Data > current.Next.Data && current.Next!=null) 
      { 
       int temp = current.Data; 
       current.Data = current.Next.Data; 
       current.Next.Data = temp; 
      } 

     } 
    } 

是完全一樣的話說:

Node current = head; 
      if (current.Data > current.Next.Data && current.Next!=null) 
      { 
       int temp = current.Data; 
       current.Data = current.Next.Data; 
       current.Next.Data = temp; 
      } 

你不改變「當前」節點,即你總是在你的列表中排序第一和第二項。

我不會給你完整的解決方案畢竟這是作業的目的。在循環中,確保當前的內容始終是列表中的第j個項目,當它開始內部循環時,您將得到正確的結果。

此外,您正在交換位稍微不正確,您應該交換節點不只是數據。即節點的下一個字段以及當前節點需要更新的點。這應該讓你更多的布朗點比交換數據。

另外一些調試提示:添加這樣一個print()函數:

public void Print() 
    { 
     Node current = head; 
     Console.Write("List: "); 
     while (current != null) 
     { 
      Console.Write("{0} ", current.Data); 
      current = current.Next; 
     } 
     Console.WriteLine(""); 
    } 

,並調用它在你的排序循環,它會幫助你瞭解該列表每次迭代之間改變..幫助你瞭解問題的癥結所在。

List: 3 1 50 2 5 4 
List: 3 1 50 2 5 4 
List: 1 3 50 2 5 4 
List: 1 3 50 2 5 4 
List: 1 3 2 50 5 4 
List: 1 3 2 5 50 4 
List: 1 3 2 5 4 50 
List: 1 2 3 5 4 50 
List: 1 2 3 5 4 50 
List: 1 2 3 4 5 50 

哦!男人..每次我閱讀代碼我發現一些新的錯誤! ...

 if (current.Data > current.Next.Data && current.Next!=null) 

應該

 if (current != null && current.Next!=null && current.Data > current.Next.Data) 

您的代碼不會崩潰,因爲它不會做的那一刻任何事情。

希望這會有所幫助。

+0

嘿!本可以更快地粘貼工作代碼;) – chkdsk 2011-03-18 17:32:57

+0

什麼是工作代碼? – ZeeeeeV 2013-03-25 11:49:39

0

另一個帶有2個屬性的簡單類的示例。這不是數組,而是一個簡單的類模擬指針......只是爲了好玩!

class MyLinkedList 
{ 
    MyLinkedList nextNode; 
    int data; 

    public void OrderListBubbleAlgoritm(ref MyLinkedList head) 
    { 
     bool needRestart = true; 
     MyLinkedList actualNode = head; //node Im working with   
     int temp; 

     while (needRestart) 
     { 
      needRestart = false; 
      actualNode = head; 
      while (!needRestart && actualNode.nextNode != null) 
      { 
       if (actualNode.nextNode.data >= actualNode.data) //is sorted 
       { 
        actualNode = actualNode.nextNode; 
       } 
       else 
       { 
        //swap the data 
        temp = actualNode.data; 
        actualNode.data = actualNode.nextNode.data; 
        actualNode.nextNode.data = temp; 

        needRestart = true; 
       } 
      } 
     } 
    } 
} 

請記住只使用少量數據的情況下使用氣泡排序。
它的表現是:O(n^2)