Can YOU bubble sort? :)
howdy, IB Programmers!
the instructor for my college's Adv. C++ class challenges his students every semester to figure out, on their own, how to bubble-sort a linked-list of C++ pointers. one or two may figure it out each time, but more often than not, no one does...
so, code gurus, do YOU know how to accomplish such a task?
Details:
1. List is of type Link.
2. Link is a struct containing a People object and a Link pointer to the next Link in line.
3. People is a class containing a String name and an int Age.
4. Must compare the name of each Link's People object to see if in alphabetical order.
5. Cannot move People around, can only reassign Link pointer.
anyone that wants it, i can probably get the original assignment sheet.
oh, and BTW, this isn't for me. i already finished this class. i'm just curious to see if any of ya'll can do this...
:D



Teej
on Nov 26, 2007
I was in the middle of writing this out when I realized I hate C++ and gave up!
louiemctool on Nov 26, 2007
haha.
i get pretty much the same response from students i try to tutor...
whatever happened to "nothing worth knowing ever comes easily"?
:D
jconstant on Nov 26, 2007
My Solution
;~P
iconmaniac on Nov 27, 2007
Here you go. Don't say I don't give you things. :)
Link* sort(Link* head) {
if (!head || !head->next) {
return head;
}
bool sorted;
do {
sorted = true;
Link** prev = &head;
Link* cur = head;
while (cur->next) {
if (cur->value > cur->next->value) {
sorted = false;
*prev = cur->next;
cur->next = cur->next->next;
(*prev)->next = cur;
}
else {
cur = cur->next;
}
prev = &(*prev)->next;
}
} while (!sorted);
return head;
}
ZsaZsa on Nov 27, 2007
I can sort of bubble. Does that count?
Betty on Nov 27, 2007
I can pop bubble wrap. Does that count?
davethegr8 on Nov 27, 2007
Yes, I can do it. Though it would take me way too long to actually do. It's actually pretty easy, you just need a compare() function in the object.
anti on Nov 27, 2007
Bubble bubble, toil and trouble.
louiemctool on Nov 27, 2007
macbeth...
:D
rajr19 on Nov 27, 2007
i blow bubbles!
louiemctool on Nov 27, 2007
sah-weeet...
:D
sloan on Nov 27, 2007
I can blow spit bubbles...
louiemctool on Nov 28, 2007
awesome!
:D
miranoriel on Feb 02, 2008
I'm bubbly!
LOL
anti on Feb 02, 2008
Go Team!
debbye on Feb 02, 2008
@Betty - wonderful BubbleWrap!
iqon on Feb 02, 2008
ARRRRR, me bubbles sort themselves
miranoriel on Feb 07, 2008
:gets out of the pool:
Hey! Who was making those bubbles?!
XD
zelda013 on Feb 07, 2008
i like to drink bubbly (champagne).
elnitido52 on Feb 07, 2008
Double-bubbles chewing gum...
jojobear99 on Feb 07, 2008
we had to do that in data structures class...even a doubly linked one...implemented both in C++ and Java...of course, they actually taught us what bubble sort was first...and even acted it out at the front of the classroom by having people with cards with numbers on them stand at the front of the room and sort themselves
samkarn on Apr 09, 2008
Solution: re-write the linked list as a normal array. Linked lists are for back when space was at a premium. Those days are gone, gone, gone...
louiemctool on Apr 09, 2008
actually, the instructor for this class is a developer for a local company that produces a lot of high-precision molding and color-analyzing hardware, among other things. he states that when you get down to a certain level of processing linked lists are actually far more accurate and efficient, and therefore preferable, than arrays.
its a case of stack vs. heap, and stack wins every time...
:)