void LLquickSort(LL_t * intlist) { // base case: 0 or 1 node if (intlist->head == intlist->tail) { return; } // remove the head from intlist (pivot) int pivot = intlist->head->data; node_t * pivot_node = intlist->head; intlist->head = intlist->head->next; // partition LL_t * first = LLcreate(); LL_t * second = LLcreate(); node_t * curr = intlist->head; node_t * next; while (curr != NULL) { next = curr->next; curr->next = NULL; if (curr->data <= pivot) { LLappendNode(first, curr); } else { LLappendNode(second, curr); } curr = next; } intlist->head = NULL; intlist->tail = NULL; //LLprint(first); //LLprint(second); LLquickSort(first); LLquickSort(second); // join LLcatenate(intlist, first); LLappendNode(intlist, pivot_node); LLcatenate(intlist, second); }
// Pre: intlist not sorted // Post: values in intlist are in sorted order void LLinsertionSort(LL_t * intlist) { // create a new [empty] list LL_t * newlist = LLcreate(); // scan through all items and insert 'em node_t * curr = intlist->head; while (curr != NULL) { node_t * next = curr->next; LLinsertNode(newlist, curr); curr = next; } // recycle old list // swap LL_t tmp; tmp = *intlist; *intlist = *newlist; *newlist = tmp; free(newlist); //LLdestroy(newlist); }
LL_t* creat(){ LL_t* s=LLcreate(); return s; }