![]() |
| | #1 |
| Registered User Join Date: Aug 2007
Posts: 36
| Code: for (i=MIN; i<MAX; i++) {
for (j=MIN; j<MAX-1; j++) {
if v[j] > v[j+1] {
aux ← v[j] ;
v[j] ← v[j+1];
v[j+1] ← aux;
}
}
}
Any ideas? |
| JOCAAN is offline | |
| | #2 |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,945
| You rearrange the "next" pointers, eg: Code: if (first->value > second->value) {
first->next=second->next;
second->next=first;
}
Or what's on third? :0
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS |
| MK27 is online now | |
| | #3 |
| Algorithm Dissector Join Date: Dec 2005 Location: New Zealand
Posts: 2,476
| It sounds like you're specifically after code for bubble-sorting doubly-linked-list via swapping pointers rather than values. I do have code for that already, but let me tell you that it is very difficult to write. I'm actually on holiday at the moment, but the code is on my website. The sorting algorithms that are easiest to write for arrays are actually often quite a bit harder for linked lists. Also those that are harder for arrays are sometimes easier for linked lists. I believe the easiest one for linked-lists is Insertion Sort, so perhaps you could try that? Merge Sort is however not much harder and is far more effective.
__________________ My homepage Advice: Take only as directed - If symptoms persist, please see your debugger Last edited by iMalc; 01-18-2009 at 12:51 PM. |
| iMalc is offline | |
| | #4 | |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,945
| Quote:
Imagine my node looks like this: Code: typedef struct _node {
char firstname[32];
char lastname[32];
char middleinitial;
int group;
float balance;
char note[256];
double special;
struct _node* next;
}
And telling someone that "a quicksort is much easier to write than a bubble sort" is another obvious piece of deception. I would love to hear from even one other person that agrees with what iMalc has written here today.
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS | |
| MK27 is online now | |
| | #5 |
| Algorithm Dissector Join Date: Dec 2005 Location: New Zealand
Posts: 2,476
| I corrected myself straight after I first posted. You must have taken over 5 minutes to post. I'm amazed you managed to see what I had written incorrectly. I realised immediately that I was thinking of one of the other sorts. You can definitely sort a doubly-linked list by swapping values. A number of coders who are used to array sorting try this as their first attempt at linked-list sorting. I certainly agree, it's a very bad idea and you've pointed out one reason why, but some people do it. The other reason is that you are stuck with algorithms that are O(n*n) because you have neither random access, nor tha ability to 'insert' items as you would with a list. I never intended to imply that swapping values would be the normal way of doing it. Edited for spelling this time. Last edited by iMalc; 01-18-2009 at 01:32 PM. |
| iMalc is offline | |
| | #6 | |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,945
| Quote:
I don't really understand the formulas used to express the efficiency of the algorithms, but I do understand the algorithms themselves in so far as they are applied in programming. I believe the O(n^2) etc style measurements of efficiency are actually based on a mathematical abstraction of the method ("algorithm") AND NOT statistical measurement of how the method performs in practice. (In other words, this is someone's deductive reasoning and not an empirical observation). After all, you could write a sort according to the algorithm and then average or measure the number of comparisons it makes. But that IS NOT how these formulas (eg. O(n^2)) came into being, and the actual logic of them is not always made apparent. For example, a normative statement about the bubble sort is that "bubble sort always performs n-1 passes through the data, period. Its best case and worst case behaviors are completely identical". I actually have several charts and tables of formuli that all say this, sometimes vehemently, and I imagine they all actually have the same source somewhere in time and space, which is never referenced because they appear to rest on the authority of deductive reasoning. Yet in practice this statement about bubble sort CANNOT BE TRUE. In practice, a flag is used to indicate the completion of a sort. Bubble sort passes thru data one item at a time and swaps it forward if it's value is higher than the next item. If, after a pass, nothing has changed, the sort is done. That means by definition bubble sort will make a variable number of passes. If the list is already sorted (a best case), then you have n-1 passes. Selection sort, which works by moving lowest values one at a time, will always have the same number of passes since it proceeds the same way regardless. So with an badly unsorted list it will tend to do better than bubble sort, but if the list is only slightly out of order, bubble sort will do much, much better, making a comparable number of passes and comparisons to a merge sort. So regurgitating these "measurements" out of a textbook tends to imply ??what?? about some of the computer science clergy, iMalc. I guess it all amounts to the same thing anyway. A merge sort is better than a selection sort is better than a bubble sort, etc.
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS | |
| MK27 is online now | |
| | #7 | |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| Quote:
| |
| tabstop is offline | |
| | #8 | |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,945
| Quote:
1) Compare two adjacent items. 2) Swap if necessary. 3) Move forward one. 4) Repeat until the list is sorted. Find me a bubble sort that does not use a flag. The other option is 4) Repeat infinitely. If "an algorithm and its implementation are not the same thing" means the difference between something that WILL NOT RUN ON A COMPUTER and something that will, then we agree. I have not "improved" the bubble sort by using a flag -- and they all do -- because otherwise an implimentation IS NOT POSSIBLE. However, I think I now understand the root of what is obviously a historical argument.
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS | |
| MK27 is online now | |
| | #9 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| That's not hard -- any bubble sort will do: Code: for start <- 0 to n-2
for i <- n-1 downto start+1
if array_(i-1) > array_i
swap array_(i-1) and array_i
|
| tabstop is offline | |
| | #10 |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,945
| Okay! So the formula is O(n^2) because this sort must iterate through the entire list a number of times equal to the length of the list (which with a flag this would be the worst case)? You may have redeemed computer science in my eyes, tabstop. But barely. ps. I thought Sedgewick was a girl in a Woodie Allen film.
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS |
| MK27 is online now | |
| | #11 | |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| Quote:
| |
| tabstop is offline | |
| | #12 |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,945
| So it's polynomial instead of exponential...
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS |
| MK27 is online now | |
| | #13 | |
| Algorithm Dissector Join Date: Dec 2005 Location: New Zealand
Posts: 2,476
| Quote:
I found that of all the sorting algorithms that are possible when you remove both random access and the ability to insert/remove in constant time from the middle of the data, are all limited to being in the O(n*n) class of algorithms or worse. I'd be pleased to be proven wrong there though, as it should mean there is an algorithm I've overlooked. I don't see any relevance in the fact that bubble sort can be optimised to perform n-1 operations in the best case. That still means it's O(n*n) because the worst case is unchanged. Saying that all implementations of bubble sort use a flag is either wrong, or rather naieve at best. That isn't even the best way to improve bubble sort. Instead of a boolean flag, you can for the same amount of effort, store the integer position that the bubble floated to, and this improves the running time for even more cases. It's still bubble sort, it just eliminates even more redundant comparisons in certain cases. Yes I possibly made a mistake, and corrected myself, when I momentarily stated that bubblesort for a linked list (by swapping pointers) is harder to write than quicksort. However as it happens, it actually is pretty damn difficult though anyway, and unless you've actually tried it (and succeeded) then you can't really say otherwise. It probably depends on the language too. If you do attempt it, be sure to test it very thoroughly, as you may think you have it right, but it might fall over in certain circumstances. If that's not hard enough for you, try shaker sort for a doubly-linked list. Now that's seriously hard to implement and get right, and I would certainly confidently say that that is harder than just implementing quicksort! Make sure you're doing it by swapping pointers only too. You should know that I am emphatically NOT someone who just quotes other literature on a subject. I am someone who always learns by doing. I remember once in a lecture on Prolog where we were all told that we should never ever use a certain construct. I argued this to death with my flatmate because before we were even told this, I had written a program which made specific use of that construct. About a month later when the lecturer thought we were at a high enough level to understand it, he told us how he had lied earlier and that there was a specific usage of the construct I mentioned earlier. I could fully understand why he had dumbed it down for us earlier, as using this construct when we didn't understand it would have been disastrous, but I discovered the correct use for it on my own. Anyway, I most certainly do not pass on knowledge I have not at least verified myself. As it happens, my claim above about being limited to O(n*n) algorithms when there is no random access or insertion is my own claim. I'm not even aware of any literature making the same claim. I'm as against people simply regurgitating information they've had shoved down their throats as much as you are. So, try not to jump to conclusions. You've no idea how many times I edited that post afterwards...
__________________ My homepage Advice: Take only as directed - If symptoms persist, please see your debugger Last edited by iMalc; 01-19-2009 at 05:35 PM. | |
| iMalc is offline | |
| | #14 | |||||||
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,945
| No, actually I've been trying to take you back from the dark side. Of course, I never really believed what I was saying, I just thought I did. I've also formed a sort of emotional defensiveness about bubblesort since the first time I had to sort anything, that's what I intuitively wrote. laserlight later identified it as such, scornfully I think -- and now tabstop says I've been using some kind of cheat flag! So when I heard somebody else in history had come up with a better method(s), I felt determined to master them. Not to be outdone as a masochist either, I'm actually writing (another) program that performs four or five different styles of searches, counts comparisons and list iterations, shuffles, generates random lists, switches criteria, breaks ties with secondary criteria, etc. Real fun, for the whole family! So I expected to see those O(n^2) tables confirmed, and thankfully tabstop cleared this flag issue up or my suspicion that many computer science gurus are more determined to obfuscate than explain would have reached critical proportions. Quote:
Quote:
Just kidding. By "insert/remove in constant time" I presume you mean the difference between a linked list and an array, where the entire array must be re-numbered. "Random access" I'll have to google...ugh. Quote:
Quote:
Quote:
Quote:
Quote:
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS Last edited by MK27; 01-19-2009 at 07:44 PM. | |||||||
| MK27 is online now | |
| | #15 |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,945
| Pursuant to all this, what's a the relative intensity of processor activity for the following actions:
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS |
| MK27 is online now | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| seg fault for linked list in another linked list--help!! | idshiy | C Programming | 5 | 11-04-2006 06:39 PM |
| Reverse function for linked list | Brigs76 | C++ Programming | 1 | 10-25-2006 10:01 AM |
| sort linked list using BST | Micko | C Programming | 8 | 10-04-2004 02:04 PM |
| Sort Linked List Algorithm. | xddxogm3 | C++ Programming | 7 | 10-13-2003 11:05 PM |
| Template Class for Linked List | pecymanski | C++ Programming | 2 | 12-04-2001 09:07 PM |