Bubblesort on the general list in C #

I work with a general list in C #, but I have a problem when I try to sort nodes using the bubble sort method.

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;
        }
     }
}

When I add more than 5 nodes to the list, the bubblesort method stops working (sorts the list incorrectly). Can anyone help?

+3
source share
5 answers

You will need to clarify what you mean by "stops working" ... fails? Core dumps? Doesn't sort the list correctly?

The problem is that you need to flip currentback on head+1at each iteration i(before the iteration j).

, j 1 size-i ( - ). j size-1 i.

: while/boolean/loop ( while, boolean, , , ). , - ( , ).

+2

, , . Google.

btw..

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

.. .

, :

()

    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;
            }

"" node, .. .

, . , "j'th , , .

, , . node Next node . , , .

: 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)

, .

, .

+1

, i j, . .

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

, , while, Size, , . bool, - , , , . , .

, , . .

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)

0

, - , :

static void BubbleSort(List<string> stringList)
        {
            List<string> bubbleList = new List<string>(stringList);
            string temp = string.Empty;

            for (int i = 1; (i <= (bubbleList.Count)); i++)
            {
                for (int j = 0; j < (bubbleList.Count - 1); j++)
                {
                    if (bubbleList[j].CompareTo(bubbleList[j + 1]) == 1)
                    {
                        temp = bubbleList[j];
                        bubbleList[j] = bubbleList[j + 1];
                        bubbleList[j + 1] = temp;
                    }
                }
            }
-1

All Articles