{"id":25245,"date":"2017-10-15T16:20:32","date_gmt":"2017-10-15T10:50:32","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=25245"},"modified":"2018-11-01T10:44:16","modified_gmt":"2018-11-01T05:14:16","slug":"quicksort-doubly-linked-list-2","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/quicksort-doubly-linked-list-2\/","title":{"rendered":"QuickSort on Doubly Linked List"},"content":{"rendered":"<p><span style=\"color: #333300;\"><strong>QuickSort on Doubly Linked List<\/strong><\/span><\/p>\n<p>Following is a typical recursive implementation of <a href=\"http:\/\/en.wikipedia.org\/wiki\/Quicksort\" target=\"_blank\" rel=\"noopener\">QuickSort<\/a> for arrays. The implementation uses last element as pivot.<span id=\"more-117304\"><\/span><\/p>\n<h3 id=\"doubly-linked-list\"><span style=\"color: #993366;\">Doubly Linked List:<\/span><\/h3>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter wp-image-31642 size-full\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/10\/doubly-linked-list.gif\" alt=\"Doubly Linked List\" width=\"600\" height=\"136\" \/><\/p>\n<div>\n[pastacode lang=\u201dcpp\u201d manual=\u201d%2F*%20A%20typical%20recursive%20implementation%20of%20Quicksort%20for%20array*%2F%0A%20%0A%2F*%20This%20function%20takes%20last%20element%20as%20pivot%2C%20places%20the%20pivot%20element%20at%20its%0A%20%20%20correct%20position%20in%20sorted%20array%2C%20and%20places%20all%20smaller%20(smaller%20than%20%0A%20%20%20pivot)%20to%20left%20of%20pivot%20and%20all%20greater%20elements%20to%20right%20of%20pivot%20*%2F%0Aint%20partition%20(int%20arr%5B%5D%2C%20int%20l%2C%20int%20h)%0A%7B%0A%20%20%20%20int%20x%20%3D%20arr%5Bh%5D%3B%0A%20%20%20%20int%20i%20%3D%20(l%20-%201)%3B%0A%20%0A%20%20%20%20for%20(int%20j%20%3D%20l%3B%20j%20%3C%3D%20h-%201%3B%20j%2B%2B)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20if%20(arr%5Bj%5D%20%3C%3D%20x)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20i%2B%2B%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20swap%20(%26arr%5Bi%5D%2C%20%26arr%5Bj%5D)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%20%20%20swap%20(%26arr%5Bi%20%2B%201%5D%2C%20%26arr%5Bh%5D)%3B%0A%20%20%20%20return%20(i%20%2B%201)%3B%0A%7D%0A%20%0A%2F*%20A%5B%5D%20\u2013%3E%20Array%20to%20be%20sorted%2C%20l%20%20\u2013%3E%20Starting%20index%2C%20h%20%20\u2013%3E%20Ending%20index%20*%2F%0Avoid%20quickSort(int%20A%5B%5D%2C%20int%20l%2C%20int%20h)%0A%7B%0A%20%20%20%20if%20(l%20%3C%20h)%0A%20%20%20%20%7B%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20int%20p%20%3D%20partition(A%2C%20l%2C%20h)%3B%20%2F*%20Partitioning%20index%20*%2F%0A%20%20%20%20%20%20%20%20quickSort(A%2C%20l%2C%20p%20-%201)%3B%20%20%0A%20%20%20%20%20%20%20%20quickSort(A%2C%20p%20%2B%201%2C%20h)%3B%0A%20%20%20%20%7D%0A%7D\u201d message=\u201dc++\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<h3 id=\"can-we-use-same-algorithm-for-linked-list\"><span style=\"color: #008080;\"><strong>Can we use same algorithm for Linked List?<\/strong><\/span><\/h3>\n<p>Following is <a href=\"https:\/\/www.wikitechy.com\/tutorials\/c++\/c++-structure-and-function\" target=\"_blank\" rel=\"noopener\">C++ implementation<\/a> for doubly linked list. The idea is simple, we first find out pointer to last node. Once we have pointer to last node, we can recursively <a href=\"https:\/\/www.wikitechy.com\/technology\/c-algorithm-merge-two-sorted-linked-lists\/\" target=\"_blank\" rel=\"noopener\">sort<\/a> the linked list using pointers to first and last nodes of linked list, similar to the above <a href=\"https:\/\/www.wikitechy.com\/technology\/c-algorithm-write-function-reverse-linked-list\/\" target=\"_blank\" rel=\"noopener\">recursive function<\/a> where we pass indexes of first and last array elements. The partition function for linked list is also similar to partition for arrays. Instead of returning index of the pivot element, it returns pointer to the pivot element. In the following implementation, <strong>quickSort() is just a wrapper function<\/strong>, the main recursive function is <strong>_quickSort()<\/strong> which is similar to quickSort() for array implementation.<\/p>\n[ad type=\u201dbanner\u201d]\n<h3 id=\"implementation-of-c-for-quicksort-on-doubly-linked-list\"><span style=\"color: #993300;\"><strong>Implementation of C++ for QuickSort on Doubly Linked List:<\/strong><\/span><\/h3>\n[pastacode lang=\u201dcpp\u201d manual=\u201d%2F%2F%20A%20C%2B%2B%20program%20to%20sort%20a%20linked%20list%20using%20Quicksort%0A%23include%20%3Ciostream%3E%0A%23include%20%3Cstdio.h%3E%0Ausing%20namespace%20std%3B%0A%20%0A%2F*%20a%20node%20of%20the%20doubly%20linked%20list%20*%2F%0Astruct%20node%0A%7B%0A%20%20%20%20int%20data%3B%0A%20%20%20%20struct%20node%20*next%3B%0A%20%20%20%20struct%20node%20*prev%3B%0A%7D%3B%0A%20%0A%2F*%20A%20utility%20function%20to%20swap%20two%20elements%20*%2F%0Avoid%20swap%20(%20int*%20a%2C%20int*%20b%20)%0A%7B%20%20%20int%20t%20%3D%20*a%3B%20%20%20%20%20%20*a%20%3D%20*b%3B%20%20%20%20%20%20%20*b%20%3D%20t%3B%20%20%20%7D%0A%20%0A%2F%2F%20A%20utility%20function%20to%20find%20last%20node%20of%20linked%20list%0Astruct%20node%20*lastNode(node%20*root)%0A%7B%0A%20%20%20%20while%20(root%20%26%26%20root-%3Enext)%0A%20%20%20%20%20%20%20%20root%20%3D%20root-%3Enext%3B%0A%20%20%20%20return%20root%3B%0A%7D%0A%20%0A%2F*%20Considers%20last%20element%20as%20pivot%2C%20places%20the%20pivot%20element%20at%20its%0A%20%20%20correct%20position%20in%20sorted%20array%2C%20and%20places%20all%20smaller%20(smaller%20than%0A%20%20%20pivot)%20to%20left%20of%20pivot%20and%20all%20greater%20elements%20to%20right%20of%20pivot%20*%2F%0Anode*%20partition(node%20*l%2C%20node%20*h)%0A%7B%0A%20%20%20%20%2F%2F%20set%20pivot%20as%20h%20element%0A%20%20%20%20int%20x%20%20%3D%20h-%3Edata%3B%0A%20%0A%20%20%20%20%2F%2F%20similar%20to%20i%20%3D%20l-1%20for%20array%20implementation%0A%20%20%20%20node%20*i%20%3D%20l-%3Eprev%3B%0A%20%0A%20%20%20%20%2F%2F%20Similar%20to%20%22for%20(int%20j%20%3D%20l%3B%20j%20%3C%3D%20h-%201%3B%20j%2B%2B)%22%0A%20%20%20%20for%20(node%20*j%20%3D%20l%3B%20j%20!%3D%20h%3B%20j%20%3D%20j-%3Enext)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20if%20(j-%3Edata%20%3C%3D%20x)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Similar%20to%20i%2B%2B%20for%20array%0A%20%20%20%20%20%20%20%20%20%20%20%20i%20%3D%20(i%20%3D%3D%20NULL)%3F%20l%20%3A%20i-%3Enext%3B%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20swap(%26(i-%3Edata)%2C%20%26(j-%3Edata))%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%20%20%20i%20%3D%20(i%20%3D%3D%20NULL)%3F%20l%20%3A%20i-%3Enext%3B%20%2F%2F%20Similar%20to%20i%2B%2B%0A%20%20%20%20swap(%26(i-%3Edata)%2C%20%26(h-%3Edata))%3B%0A%20%20%20%20return%20i%3B%0A%7D%0A%20%0A%2F*%20A%20recursive%20implementation%20of%20quicksort%20for%20linked%20list%20*%2F%0Avoid%20_quickSort(struct%20node*%20l%2C%20struct%20node%20*h)%0A%7B%0A%20%20%20%20if%20(h%20!%3D%20NULL%20%26%26%20l%20!%3D%20h%20%26%26%20l%20!%3D%20h-%3Enext)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20struct%20node%20*p%20%3D%20partition(l%2C%20h)%3B%0A%20%20%20%20%20%20%20%20_quickSort(l%2C%20p-%3Eprev)%3B%0A%20%20%20%20%20%20%20%20_quickSort(p-%3Enext%2C%20h)%3B%0A%20%20%20%20%7D%0A%7D%0A%20%0A%2F%2F%20The%20main%20function%20to%20sort%20a%20linked%20list.%20It%20mainly%20calls%20_quickSort()%0Avoid%20quickSort(struct%20node%20*head)%0A%7B%0A%20%20%20%20%2F%2F%20Find%20last%20node%0A%20%20%20%20struct%20node%20*h%20%3D%20lastNode(head)%3B%0A%20%0A%20%20%20%20%2F%2F%20Call%20the%20recursive%20QuickSort%0A%20%20%20%20_quickSort(head%2C%20h)%3B%0A%7D%0A%20%0A%2F%2F%20A%20utility%20function%20to%20print%20contents%20of%20arr%0Avoid%20printList(struct%20node%20*head)%0A%7B%0A%20%20%20%20while%20(head)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20cout%20%3C%3C%20head-%3Edata%20%3C%3C%20%22%20%20%22%3B%0A%20%20%20%20%20%20%20%20head%20%3D%20head-%3Enext%3B%0A%20%20%20%20%7D%0A%20%20%20%20cout%20%3C%3C%20endl%3B%0A%7D%0A%20%0A%2F*%20Function%20to%20insert%20a%20node%20at%20the%20beginging%20of%20the%20Doubly%20Linked%20List%20*%2F%0Avoid%20push(struct%20node**%20head_ref%2C%20int%20new_data)%0A%7B%0A%20%20%20%20struct%20node*%20new_node%20%3D%20new%20node%3B%20%20%20%20%20%2F*%20allocate%20node%20*%2F%0A%20%20%20%20new_node-%3Edata%20%20%3D%20new_data%3B%0A%20%0A%20%20%20%20%2F*%20since%20we%20are%20adding%20at%20the%20begining%2C%20prev%20is%20always%20NULL%20*%2F%0A%20%20%20%20new_node-%3Eprev%20%3D%20NULL%3B%0A%20%0A%20%20%20%20%2F*%20link%20the%20old%20list%20off%20the%20new%20node%20*%2F%0A%20%20%20%20new_node-%3Enext%20%3D%20(*head_ref)%3B%0A%20%0A%20%20%20%20%2F*%20change%20prev%20of%20head%20node%20to%20new%20node%20*%2F%0A%20%20%20%20if%20((*head_ref)%20!%3D%20%20NULL)%20%20(*head_ref)-%3Eprev%20%3D%20new_node%20%3B%0A%20%0A%20%20%20%20%2F*%20move%20the%20head%20to%20point%20to%20the%20new%20node%20*%2F%0A%20%20%20%20(*head_ref)%20%20%20%20%3D%20new_node%3B%0A%7D%0A%20%0A%2F*%20Driver%20program%20to%20test%20above%20function%20*%2F%0Aint%20main()%0A%7B%0A%20%20%20%20struct%20node%20*a%20%3D%20NULL%3B%0A%20%20%20%20push(%26a%2C%205)%3B%0A%20%20%20%20push(%26a%2C%2020)%3B%0A%20%20%20%20push(%26a%2C%204)%3B%0A%20%20%20%20push(%26a%2C%203)%3B%0A%20%20%20%20push(%26a%2C%2030)%3B%0A%20%0A%20%20%20%20cout%20%3C%3C%20%22Linked%20List%20before%20sorting%20%5Cn%22%3B%0A%20%20%20%20printList(a)%3B%0A%20%0A%20%20%20%20quickSort(a)%3B%0A%20%0A%20%20%20%20cout%20%3C%3C%20%22Linked%20List%20after%20sorting%20%5Cn%22%3B%0A%20%20%20%20printList(a)%3B%0A%20%0A%20%20%20%20return%200%3B%0A%7D\u201d message=\u201dc++\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<h3 id=\"output\"><span style=\"color: #003366;\"><strong>Output :<\/strong><\/span><\/h3>\n<pre>Linked List before sorting\r\n30  3  4  20  5\r\nLinked List after sorting\r\n3  4  5  20  30<\/pre>\n<p><span style=\"color: #008000;\"><strong>Time Complexity: <\/strong><\/span>Time complexity of the above implementation is same as time complexity of QuickSort() for arrays. It takes<strong> O(n^2)<\/strong> time in worst case and <strong>O(nLogn)<\/strong> in average and best cases. The worst case occurs when the <a href=\"https:\/\/www.wikitechy.com\/technology\/java-algorithm-swap-nodes-linked-list-without-swapping-data\/\" target=\"_blank\" rel=\"noopener\">linked list<\/a> is already sorted.<\/p>\n<h3 id=\"can-we-implement-random-quick-sort-for-linked-list\"><span style=\"color: #ff0000;\">Can we implement random quick sort for linked list?<\/span><\/h3>\n<p><a href=\"https:\/\/www.wikitechy.com\/tutorials\/c-programming\/quicksort-in-chttps:\/\/www.wikitechy.com\/tutorials\/c-programming\/quicksort-in-c\" target=\"_blank\" rel=\"noopener\">Quicksort<\/a> can be implemented for Linked List only when we can pick a fixed point as pivot (like last element in above implementation). Random QuickSort cannot be efficiently implemented for Linked Lists by picking random pivot.<\/p>\n[ad type=\u201dbanner\u201d]\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>QuickSort on Doubly Linked List &#8211; Searching and sorting -. The idea is simple, we first find out pointer to last node. Once we have pointer to last node, we can recursively sort the linked list using pointers to first and last nodes of linked list.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[83511,83502,71670],"tags":[71706,72507,71723,72512,72514,72420,72418,72417,71720,72511,72510,72416,70933,72412,72398,71262,72513,72505,72508,71267,71713,72219,71172,71693,72426,72415,72215,70046,71714,71712,71702,71717,71721,72413,71145,72509,72425,71303,71265,72216,72224,72432,71677,71722,71696,72430,71719,70977,72504,72419,72217,72506,72144,71299,71676,70020,71142],"class_list":["post-25245","post","type-post","status-publish","format-standard","hentry","category-linked-lists","category-quick-sort","category-searching-and-sorting","tag-algorithm-for-quicksort","tag-algorithm-for-quicksort-in-data-structure","tag-algorithm-of-quicksort","tag-algorithm-of-quicksort-in-data-structure","tag-algorithm-quicksort","tag-c-program-for-quicksort-using-arrays","tag-c-quicksort","tag-c-quicksort-example","tag-example-of-quicksort","tag-explain-quick-sort-with-example-in-data-structure","tag-explain-quicksort","tag-explain-quicksort-with-example","tag-heap-sort-program-in-c","tag-java-quicksort","tag-linked-list-sort","tag-merge-sort-algorithm","tag-program-for-quicksort-in-data-structure","tag-python-quicksort","tag-qsort-algorithm","tag-quick-sort-algorithm","tag-quick-sort-c-program","tag-quick-sort-logic","tag-quick-sort-program","tag-quick-sort-program-in-c","tag-quick-sort-program-in-data-structure-using-c","tag-quick-sort-program-using-c","tag-quick-sort-pseudocode","tag-quicksort","tag-quicksort-algorithm-in-data-structure","tag-quicksort-algorithm-with-example","tag-quicksort-algorithm-with-example-in-data-structure","tag-quicksort-animation","tag-quicksort-c-code","tag-quicksort-c-code-example","tag-quicksort-c","tag-quicksort-c-example","tag-quicksort-c-program-code","tag-quicksort-code","tag-quicksort-example","tag-quicksort-example-step-by-step","tag-quicksort-example-step-by-step-animation","tag-quicksort-example-step-by-step-explanation","tag-quicksort-explained","tag-quicksort-explanation-with-example","tag-quicksort-in-c","tag-quicksort-in-c-code","tag-quicksort-in-data-structure-with-example","tag-quicksort-java","tag-quicksort-on-doubly-linked-list","tag-quicksort-partition","tag-quicksort-python","tag-quicksort-simulation","tag-quicksort-visualization","tag-sort-linked-list","tag-sorting-a-linked-list","tag-sorting-algorithms","tag-sorting-in-data-structure"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25245","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=25245"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25245\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=25245"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=25245"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=25245"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}