I'm currently learning about many different ways of sorting data. I'm trying to implement insertion sort on doubly linked list using C++. I understand the concept of using it on an array. But, i'm having difficult time figuring out why my "while" loop is looping indefinitely.
Here is my code :
template <class T>
class Node
{
protected:
T item;
Node<T>* prev;
Node<T>* next;
public :
Node()
{
prev=nullptr;
next=nullptr;
}
};
//InsertionSort
void insertionSort()
{
Node<T>* current = head->next;
int count = 1;
for(current; current != nullptr; current=current->next)
{
Node<T>* j = current;
Node<T>* bj = j->prev;
while(j != nullptr && j->item < bj->item)
{
cout << count++ << "Hi" << endl;
Node<T>* temp = j->next;
j->next = bj;
bj->prev = j;
bj->next = temp;
temp->prev = bj;
}
}
}
I added int count to see how many times my loops occur. My for loop is supposed to occur 920 times, and it does. But, my while loop seems to be looping for indefinite amount of times.
Please help.