Any one implement iMalc super quick sort and actually get it to be functionall?
I've got some questions for Malcom, If he would be able to spend a moment to assist?
first let me say, I'm trying to implement the sort using standard C.
Within the aux function: what is the point of the *prev ? its not used ?Code:#include <stdio.h> #include <unistd.h> typedef struct node { struct node* prev; struct node* next; void *data; } node_t; void* node_get(node_t* n) { return (n->data); } static void list_partition( node_t *start , node_t *split , node_t *less_n , node_t *grtr_n , int comp(const void *, const void *) ) { do { node_t *next = start->next; if ( comp(node_get(start), node_get(split)) < 0 ) { start->next = less_n; less_n = start; } else { start->next = grtr_n; grtr_n = start ; } start = next; } while (start != NULL ); return ; } static node_t * list_SQsortAux( node_t *head , int comp(const void *, const void *) ) { node_t *first = head; node_t *second = head->next; node_t *less = NULL, *center = NULL, *greatereq = NULL ; if(second != NULL ) { do { /* find a splitter node - the first whose next item is smaller */ if (comp(node_get(second), node_get(first)) < 0 ) { /* Splitter found */ greatereq = center = first ; first->next = NULL; less = head; /* Partition the list */ list_partition(second, center, less, greatereq, comp ); node_t *const join = list_SQsortAux(less, comp ); node_t *const last = list_SQsortAux(greatereq, comp ); /* join the two sorted lists */ head = less ; join->next = greatereq ; return last; } /* Still searching for splitter */ first = second; second = second->next; }while( second != NULL ) ; } /* Already sorted, No split node found */ return first ; } void list_SQsort(node_t *ll, int comp(const void *, const void *) ) { if (ll != NULL ) { list_SQsortAux(ll, comp ); } }
I think there is a flaw in the design.. Consider the following added to a list,
50, 20, 10, 15, 55
The Spitt node is found for 50, this is also becomes the head, first, center and greater.
And the partitioning when it encounters 55 will be incorrect as it will attempt to add it to the same stack as less, except less ptr has now moved.
Any ideas on how to resolve this? Any Improvements for a double linked list, where a sentinel node is used ?
Thanks



LinkBack URL
About LinkBacks



, reversed(20%) or mixed(10%).