Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Programming Langauge C++ I need help with my bubble sort in a linked list. Using

ID: 3828090 • Letter: P

Question

Programming Langauge C++

I need help with my bubble sort in a linked list. Using the Code Provided in the Bubble sort function, i included my header files below, I just need help making this bubble sort working. Everything else in the program is done. I'm not sure if i'm doing this correctly.

int BubbleSort()
{
   int iCompareCount = 0;                                       // total # of comparisons
   NodeClass* tempNode = new NodeClass;           // hold item to be compared
   string strData;                                               // hold string to be compared
   int iNodeCount = 0;                                           // length of linked list
   bool isSorted = false;                                       // if a move needs to happen

                                                               // count number of nodes
   list->SetCurrentNode(list->GetFirstNode());                   // start at top
   list->Next();                                               // start with the 2nd node


   while (list->GetCurrentNode() != nullptr)                   // run through list
   {
       iNodeCount++;                                           // count
       //list->Next();                                           // move
   }

   for (int iteration = 1; iteration < iNodeCount && !isSorted; iteration++)
   {
       isSorted = true;
          
       for (int index = 0; index < iNodeCount - iteration; index++)
       {
           iCompareCount++;

           if (list->GetCurrentNode() > list->GetCurrentNode()->GetNextNode())   // top of list
           {  
               tempNode = list->GetFirstNode();
               list->GetCurrentNode() == list->GetCurrentNode()->GetNextNode();
               list->GetCurrentNode()->GetNextNode() == tempNode;
               list->GetCurrentNode()->GetNextNode();
              
               isSorted = false;
           }
              

           if(isSorted)
                                                                   // yes, move the temp node
               {
                   list->Insert(strData);                           // insert this node
                   list->SetCurrentNode(tempNode);                   // reset location
                   list->Delete();                                   // delete this node we just moved
                   tempNode = list->GetCurrentNode();               // re-save temp location
                   break;                                           // done with this do loop
               }

       }
   }
  
   return iCompareCount;
}

----------------------------------------------------------------------------------------------------------------------

LinkedListClass.h

#pragma once
class LinkedListClass
{
public:
   LinkedListClass();
   ~LinkedListClass();

   void Append(string);
   void Insert(string);
   void Delete();
   void Next();
   void Previous();

   NodeClass* GetFirstNode();
   NodeClass* GetLastNode();
   NodeClass* GetCurrentNode();
   void SetCurrentNode(NodeClass*);

private:
   NodeClass* firstNode;
   NodeClass* lastNode;
   NodeClass* currentNode;

};

NodeClass.h

class NodeClass
{
public:
   NodeClass();
   ~NodeClass();

   void SetString(string);               // sets strString
   string GetString();                   // returns strString

   void SetNextNode(NodeClass*);       // sets pointer to next node
   void SetPrevNode(NodeClass*);       // sets pointer to previous node
   NodeClass* GetNextNode();           // returns pointer to next node
   NodeClass* GetPrevNode();           // returns pointer to previous node

private:
   string strText;                       // string from file

   NodeClass* nextNode;               // pointer to next node
   NodeClass* prevNode;               // pointer to [revopios node
};

Explanation / Answer

//Modified code for bubble sorting.

int BubbleSort()
{
int iCompareCount = 0; // total # of comparisons
NodeClass* tempNode = new NodeClass; // hold item to be compared
string strData; // hold string to be compared
int iNodeCount = 0; // length of linked list
bool isSorted = false; // if a move needs to happen
// count number of nodes
list->SetCurrentNode(list->GetFirstNode()); // start at top
list->Next(); // start with the 2nd node

while (list->GetCurrentNode() != nullptr) // run through list
{
iNodeCount++; // count
list->Next(); // move
}
list->SetCurrentNode(list->GetFirstNode()); //Reset pointer to first node in list for sorting
for (int iteration = 1; iteration < iNodeCount; iteration++)
{
// isSorted = true;
  
for (int index = 0; index < iNodeCount - iteration; index++)
{
iCompareCount++;
NodeClass * t1=list->GetCurrentNode();
NodeClass * t2=list->GetCurrentNode()->GetNextNode();
if (t1->GetString() > t2->GetString()) // top of list
{
tempNode = t1;
t2->SetPrevNode(t1->GetPrevNode());
tempNode->SetPrevNode(t2);
tempNode->SetNextNode(t2->GetNextNode());
t2->SetNextNode(tempNode);
list->SetCurrentNode(t1);
// isSorted = false;
}
  
/* if(isSorted)
// yes, move the temp node
{
list->Insert(strData); // insert this node
list->SetCurrentNode(tempNode); // reset location
list->Delete(); // delete this node we just moved
tempNode = list->GetCurrentNode(); // re-save temp location
break; // done with this do loop
}
*/}
}
  
return iCompareCount;
}