T3rminal
asked on
Sorting a linked list with bubblesort
Hi,
I'm trying to sort a linear linked list by count from the node below. Ive tried using a bubble sort but cant seem to get it working, pasted below also, any suggestions would be great.
struct node {
char string25[26];
int count;
struct node * next;
};
After adding some nodes I have a linear list so set curr to head
curr = head;
while (head != NULL) {
head = curr;
while(head != NULL) {
if(curr->count < curr->next->count)
curr = curr->next;
}
curr = curr->next;
}
Thanks
T3
I'm trying to sort a linear linked list by count from the node below. Ive tried using a bubble sort but cant seem to get it working, pasted below also, any suggestions would be great.
struct node {
char string25[26];
int count;
struct node * next;
};
After adding some nodes I have a linear list so set curr to head
curr = head;
while (head != NULL) {
head = curr;
while(head != NULL) {
if(curr->count < curr->next->count)
curr = curr->next;
}
curr = curr->next;
}
Thanks
T3
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
thanks kdo
ya, it is not a bubble sort. but this is easy for a beginer, even though still i use this sort when number of element is capable to handle.
~marchent~
ya, it is not a bubble sort. but this is easy for a beginer, even though still i use this sort when number of element is capable to handle.
~marchent~
ASKER
Hi,
Sorry for the vague first post. So far I have this but still cant seem to get it working, after enterting the sort I am unable to print anything to screen. Ultimately I'd just like to sort by count, perhaps bubble sort is not the best method for this? I didnt think an Insertion Sort would work as its possible I'll have 2 counts which will be the same.
curr = head;
while (curr && curr->next) {
if (curr->count <= curr->next->count)
continue;
temp = curr->next;
curr->next = curr->next->next;
curr2 = head;
while (curr2->count < temp->count)
curr2 = curr2->next;
temp->next = curr2->next;
curr2->next = temp;
}
Sorry for the vague first post. So far I have this but still cant seem to get it working, after enterting the sort I am unable to print anything to screen. Ultimately I'd just like to sort by count, perhaps bubble sort is not the best method for this? I didnt think an Insertion Sort would work as its possible I'll have 2 counts which will be the same.
curr = head;
while (curr && curr->next) {
if (curr->count <= curr->next->count)
continue;
temp = curr->next;
curr->next = curr->next->next;
curr2 = head;
while (curr2->count < temp->count)
curr2 = curr2->next;
temp->next = curr2->next;
curr2->next = temp;
}
Hi Marchent,
A couple of things about your algorithm. Well... 4.
-- You've proposed a successive maximum sort. That's not a bubble sort.
-- The test is backwards (sorts descending instead of ascending)
-- No provision is made for changing the item at head.
-- swap() has no good implementation. To swap items, you need to pass the node prior to the nodes to swap.
Good try though. :)
Kent