Can anyone help..why isn't this bubble sort working?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
void LList :: bubbleSort (LList A, int n)
{
Node *temp;
temp = Head;
int hold;
for (int pass = 1; pass <= n-1; pass++) // number of passes needed for bubblesort is the number of elements-1
{
for (int c = 0; c < n-pass; c++)//only runs for unsorted elements
{
if ((temp->data) > (temp->link->data))//if the score to the right is greater the below swaps it
{
hold = temp->data;
temp->data = temp->link->data;
temp->link->data = hold;
}
temp = temp->link;
It is passed the LList, and the number of items in the list...
Also, this function is part of my LList public functions...so head is equal to the caller's head...Still having an issue, can't seem to figure out why its not working.
template <typename T>
void LList <T> :: bubbleSort (int n) //bubbleSort for linked list
{
Node <T> *temp; //temp Node pointer
temp = Head; //start at the Head
int hold; //hold variable
for (int pass = 1; pass <= n-1; pass++) // number of passes needed for bubblesort is the number of elements-1
{
for (int c = 0; c < n-pass; c++)//only runs for unsorted elements
{
if ((temp->data) > (temp->link->data) && (temp->link !=NULL))//if the score to the right (next) is greater the below swaps it
{
hold = temp->data;
temp->data = temp->link->data;
temp->link->data = hold;
}
temp = temp->link;
}
}
}