![]() |
| | #16 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,359
| Quote:
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way | |
| laserlight is online now | |
| | #17 |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,944
| So...rather than rewrite the linked-list out into fresh halfs, I could just use a counter to work with a half at a time?
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS |
| MK27 is offline | |
| | #18 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,359
| Quote:
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way | |
| laserlight is online now | |
| | #19 |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,944
| Whew! I got the merge sort, and I'd love it if someone could glance throught the code to see if they can spot something less than praise worthy. It will work with the above script but because it can't be done without creating some kind of additional list, and I couldn't get a plain array o' struct pointers to work for me when passed aroundf, you need to add: Code: struct nodeptr {
struct _node *np;
};
Code: int LLsplitsort (struct nodeptr *retlist, struct nodeptr *left, int Llen, struct nodeptr *right, int Rlen) {
int i=0, LC=0, RC=0;
while ((Llen>LC) && (Rlen>RC)) {
if (comparenames(left[LC].np->name,right[RC].np->name)==1) {
retlist[i].np=left[LC].np;
LC++;}
else {
retlist[i].np=right[RC].np;
RC++;}
i++;
}
while (Llen>LC) {retlist[i].np=left[LC].np;i++;LC++;}
while (Rlen>RC) {retlist[i].np=right[RC].np;i++;RC++;}
return 0;
}
int LLmergesort (struct nodeptr *retlist, node *head, int len, int tag) {
int i, m=len/2, R=m, retv;
node *middle, *now, *start=head;
struct nodeptr *left, *right;
if ((left=malloc(m*sizeof(*left)))==NULL) return -1;
if ((right=malloc((m+1)*sizeof(*right)))==NULL) return -1;
if (tag==0) { // first iteration
head=head->next; //uses an empty head
if ((retlist=malloc(len*sizeof*retlist))==NULL) return -1;
}
tag++;
if (len==1) return tag; // depth of error
if (len%2>0) R++;
now=head;
if (len<4) middle=head->next;
else for (i=0;i<=m;i++) {
middle=now;
now=now->next;
}
if (m==1) left[0].np=head;
else if ((retv=LLmergesort(left,head,m,tag))!=0) return retv;
if (R==1) right[0].np=middle;
else if ((retv=LLmergesort (right,middle,R,tag))!=0) return retv;
LLsplitsort (retlist,left,m,right,R);
free(left);
free(right);
if (--tag==0) { // THE END
start->next=retlist[0].np;
for (i=0;i<len-1;i++) retlist[i].np->next=retlist[i+1].np;
free(retlist);
}
return 0;
}
if ((tmp=LLmergesort(NULL,head,num,0))!=0) printf("!! LLmergesort() error at depth %d...\n",tmp); My result was correctly sorted and made 32 comparisons (13 elements). The same list bubble sorted made 130 comparisons, and assembled with the insertion sort above, 39 comparisons. However, the insertion sort seems kind of better since it builds the list from empty (merge sort just sorts) and doesn't require a temp list while sorting. If I can think of some more names I will try it with a greater number of elements and see what's up. ![]() ![]()
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS Last edited by MK27; 12-02-2008 at 02:49 PM. |
| MK27 is offline | |
| | #20 | ||
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,359
| Quote:
Quote:
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way | ||
| laserlight is online now | |
| | #21 | |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,944
| I mean that the insertion sort will be better if the list is already sorted and you are adding a new element, so if you have an empty list, it might be better to add them one at a time with the insertion sort than to just make a list and merge sort it, because the merge sort is kind of overkill for adding one element. Quote:
In each pass, we are merging lists of size K into lists of size 2K. (Initially K equals 1.) So we start by pointing a temporary pointer p at the head of the list, and also preparing an empty list L which we will add elements to the end of as we finish dealing with them. Then: I did try it without one, but if you look at the nature of the recursion I think it will be impossible. Watch what happens if you try changing any of the pointers before the sort is complete! However, you don't need to make two linked lists; you just need an array with the pointers in the proper order. Which is to say I didn't use a temporary linked list, I used a temporary list (array). edit: But at 17 names, the insert/build does 70 comparisons while the build then merge sort only does 51... edit: at 21 names, the difference is 117 to 71 (poor ol' Bubble Sort is now 357)...god this fun
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS Last edited by MK27; 12-02-2008 at 01:45 PM. | |
| MK27 is offline | |
| | #22 | |||
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,359
| Quote:
Quote:
Quote:
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way | |||
| laserlight is online now | |
| | #23 |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,944
| Maybe. I actually didn't want to look at it first, but I will concede that Simon perhaps did somewhat of a better job.
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS |
| MK27 is offline | |
![]() |
| Tags |
| link list, pointer |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| reading data from a file - link list | peter_hii | C++ Programming | 7 | 10-25-2006 09:11 AM |
| airport Log program using 3D linked List : problem reading from file | gemini_shooter | C Programming | 3 | 03-04-2005 02:46 PM |
| compiler build error | KristTlove | C++ Programming | 2 | 11-30-2003 10:16 AM |
| 1st Class LIST ADT | Unregistered | C++ Programming | 1 | 11-09-2001 07:29 PM |
| singly linked list | clarinetster | C Programming | 2 | 08-26-2001 10:21 PM |