2011-03-18 56 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)