Pergunta

I've been trying to get bubble sort a double link list, and i read on the internet to get it working with a single linked list would be easier, so i tried that and this is my code

public void bubbleSort()
    {
        StudentNode n;
        boolean isSorted=false;

        for(int i=lenght-1; i>1 && isSorted==false; i--)
        {            
            for(int j=0; j<i; j++)
            {
                n=L.head;
                isSorted=true;
                for(int k=0; k n.next.data)
                {
                    isSorted=false;
                    swap(n, n.next);
                }
            }
        }
    }

    public void swap(StudentNode N1, StudentNode N2)
    {
        int temp=N1.data;
        N1.data=N2.data;
        N2.data=temp;

    }

How would i change it to work with a double linked list?

Foi útil?

Solução

From your implementation point of view, it really doesn't matter whether it's doubly linked list or not, for the sorting(bubbling) part at least as you are simply changing the content instead of pointers.

Outras dicas

If you only change the content (data) of each node, and don't move the nodes themselves, I don't see any reason to change anything.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top