O(nlogn)时间复杂度 链表排序

来源:互联网 发布:shell脚本调用java类 编辑:程序博客网 时间:2024/05/20 16:32

可以使用归并排序

int counter = 0;// statisticstruct list_node{int val;struct list_node* next;list_node(int t, struct list_node* pn = 0){val = t;next = pn;}};typedef list_node* list;void insert_list(list& h, int t){if (0 == h){h = new list_node(t);return;}list pn = new list_node(t, h);h = pn;}// find the middle node of the listlist find_mid(list h){list pn = h;if (0 == pn->next || 0 == pn) return pn;while(h != 0 && h->next != 0)// && h->next->next != 0) // it is needless{pn = pn->next;h = h->next->next;}return pn;}list merge_list(list h1, list h2){list hret = (0 != h1) ? h1:h2;// head of the returned listif (0 == hret) return hret;hret = (h1->val < h2->val) ? h1:h2;if (hret == h1)h1 = h1->next;else h2 = h2->next;list ret = hret; // the last node of the returned listwhile( (0 != h1) && (0 != h2) ){counter ++;// no usage, just for statisticif (h1->val < h2->val){ret->next = h1;ret = h1;// reassign reth1 = h1->next;}else{ret->next = h2;ret = h2;// reassign reth2 = h2->next;}}// attach the remained nodes to the ret listif (0 != h1)ret->next = h1;else if(0 != h2)ret->next = h2;else ret->next = 0; // 0 is the end of the returned listreturn hret;}list merge_sort_list(list h){if (0==h ||0==h->next) return h;list mid1 = find_mid(h);if (0 == mid1) return 0;list mid = mid1->next;mid1->next = 0;// break up the list h into h and midlist h1 = merge_sort_list(h);list h2 = merge_sort_list(mid);counter++;// no usage, just for statisticreturn merge_list(h1, h2);}int main(int argc, char* argv[]){list h = 0;// necessary to initialize, else h1 equal to 0xccccccccfor (int i=0 ;i< 4096; ++i)insert_list(h, rand()%100000);list ret = merge_sort_list(h);cout<<counter<<endl;// show how many operationswhile(0 != ret){cout<<ret->val<<endl;ret = ret->next;}    return 0;}