I’ve been struggling getting my bubble sort to work and now it seems to be caught in an infinite loop.
I thought I was incrementing the pointers appropriately to take care of that but it would seem not. If anyone could spot where my incrementer should go to make it exit appropriately that’d be great!
bubblesort method:
public static void bubbleSort(DoubleLinkedList list) //static method used to sort the linked list using bubble sort
{
int i = 0;
int j = 0;
Node currentNode = list.head;
Node previousNode = currentNode;
Node tempNext = currentNode;
Node tempPrevious = currentNode;
Node sentinelNode = currentNode;
for(i=1; i<list.getSize(); i++)
{
while(sentinelNode.getNext() != null)
{
if(currentNode.getData() > currentNode.getNext().getData())
{
if(currentNode == list.head)
{
Node tempNode = currentNode.getNext();
list.head = tempNode;
tempNext = tempNode.getNext();
tempNode.setNext(currentNode);
currentNode.setNext(tempNext);
currentNode.setPrevious(tempNode);
tempNext.setPrevious(currentNode);
}
else if(currentNode.getNext() == list.last)
{
Node tempNode = currentNode.getNext();
list.last = currentNode;
tempPrevious = currentNode.getPrevious();
tempNode.setNext(currentNode);
tempNode.setPrevious(tempPrevious);
currentNode.setPrevious(tempNode);
}
else
{
Node tempNode = currentNode.getNext();
tempPrevious = currentNode.getPrevious();
tempNext = currentNode.getNext();
tempPrevious.setNext(tempNode);
tempNext.setPrevious(currentNode);
currentNode.setNext(tempNext);
tempNode.setPrevious(tempPrevious);
}
}
sentinelNode = sentinelNode.getNext();
}
}
}
Your problem is in your else clause. Look closely at what your are setting. First off you have,
They are pointing to the same object. Also when you do
You aren’t changing anything because tempNode is already current.getNext();
It is because of this that your sentinelNode is having problems iterating through the DoubleLinkedList.