{"id":25419,"date":"2017-10-15T18:29:06","date_gmt":"2017-10-15T12:59:06","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=25419"},"modified":"2018-10-29T13:39:28","modified_gmt":"2018-10-29T08:09:28","slug":"java-programming-merge-sort-doubly-linked-list","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/java-programming-merge-sort-doubly-linked-list\/","title":{"rendered":"Merge Sort for Doubly Linked List"},"content":{"rendered":"<p>Given a <a href=\"https:\/\/www.wikitechy.com\/technology\/merge-sort-doubly-linked-list-2\/\" target=\"_blank\" rel=\"noopener\">doubly linked list<\/a>, write a function to sort the doubly linked list in increasing order using <a href=\"https:\/\/www.wikitechy.com\/technology\/merge-sort-linked-lists-2\/\" target=\"_blank\" rel=\"noopener\">merge sort<\/a>.<\/p>\n<p><strong>For example<\/strong>, the following doubly linked list should be changed to 2<->4<->8<->10<\/p>\n<p>The important change here is to modify the previous pointers also when merging two lists.<\/p>\n<h2 id=\"doubly-linked-list-dll\"><span style=\"color: #000080;\">Doubly Linked List (DLL):<\/span><\/h2>\n<p>Doubly Linked List (DLL) is a list of elements and it varies from <a href=\"https:\/\/www.wikitechy.com\/technology\/java-programming-union-intersection-two-linked-lists\/\" target=\"_blank\" rel=\"noopener\">Linked List<\/a>. It allows navigation, <strong>either forward or backward<\/strong> when compared to <a href=\"https:\/\/www.wikitechy.com\/technology\/insertion-sort-singly-linked-list\/\" target=\"_blank\" rel=\"noopener\">Single Linked List<\/a>.\u00a0It has two pointers:<strong> previous pointer and next pointer<\/strong>.\u00a0Every element points to next of the list and previous element in list.<\/p>\n<p><strong>Terms used in doubly linked list:<\/strong><\/p>\n<ul>\n<li>Link<\/li>\n<li>Next<\/li>\n<li>Prev<\/li>\n<li>Linked list<\/li>\n<\/ul>\n<figure id=\"attachment_31642\" aria-describedby=\"caption-attachment-31642\" style=\"width: 626px\" class=\"wp-caption aligncenter\"><img fetchpriority=\"high\" decoding=\"async\" class=\" wp-image-31642\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/10\/doubly-linked-list.gif\" alt=\"Doubly Linked List\" width=\"626\" height=\"142\" \/><figcaption id=\"caption-attachment-31642\" class=\"wp-caption-text\">Doubly Linked List<\/figcaption><\/figure>\n<h3 id=\"java-programming-implementation-of-merge-sort-for-doubly-linked-list\"><span style=\"color: #993300;\"><strong>JAVA Programming Implementation of merge sort for doubly linked list:<\/strong><\/span><\/h3>\n[pastacode lang=\u201djava\u201d manual=\u201d%0A%0Aclass%20LinkedList%20%7B%0A%20%0A%20%20%20%20static%20Node%20head%3B%20%20%0A%20%0A%20%20%0A%20%20%20%20static%20class%20Node%20%7B%0A%20%0A%20%20%20%20%20%20%20%20int%20data%3B%0A%20%20%20%20%20%20%20%20Node%20next%2C%20prev%3B%0A%20%0A%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20Node(int%20d)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20data%20%3D%20d%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20next%20%3D%20prev%20%3D%20null%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20void%20print(Node%20node)%20%7B%0A%20%20%20%20%20%20%20%20Node%20temp%20%3D%20node%3B%0A%20%20%20%20%20%20%20%20System.out.println(%22Forward%20Traversal%20using%20next%20pointer%22)%3B%0A%20%20%20%20%20%20%20%20while%20(node%20!%3D%20null)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20System.out.print(node.data%20%2B%20%22%20%22)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20temp%20%3D%20node%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20node%20%3D%20node.next%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20System.out.println(%22%5CnBackward%20Traversal%20using%20prev%20pointer%22)%3B%0A%20%20%20%20%20%20%20%20while%20(temp%20!%3D%20null)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20System.out.print(temp.data%20%2B%20%22%20%22)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20temp%20%3D%20temp.prev%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%0A%20%20%0A%20%20%20%20Node%20split(Node%20head)%20%7B%0A%20%20%20%20%20%20%20%20Node%20fast%20%3D%20head%2C%20slow%20%3D%20head%3B%0A%20%20%20%20%20%20%20%20while%20(fast.next%20!%3D%20null%20%26%26%20fast.next.next%20!%3D%20null)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20fast%20%3D%20fast.next.next%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20slow%20%3D%20slow.next%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20Node%20temp%20%3D%20slow.next%3B%0A%20%20%20%20%20%20%20%20slow.next%20%3D%20null%3B%0A%20%20%20%20%20%20%20%20return%20temp%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20Node%20mergeSort(Node%20node)%20%7B%0A%20%20%20%20%20%20%20%20if%20(node%20%3D%3D%20null%20%7C%7C%20node.next%20%3D%3D%20null)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20node%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20Node%20second%20%3D%20split(node)%3B%0A%20%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20node%20%3D%20mergeSort(node)%3B%0A%20%20%20%20%20%20%20%20second%20%3D%20mergeSort(second)%3B%0A%20%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20return%20merge(node%2C%20second)%3B%0A%20%20%20%20%7D%0A%20%0A%20%0A%20%20%20%20Node%20merge(Node%20first%2C%20Node%20second)%20%7B%0A%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20if%20(first%20%3D%3D%20null)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20second%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%0A%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20if%20(second%20%3D%3D%20null)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20first%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%0A%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20if%20(first.data%20%3C%20second.data)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20first.next%20%3D%20merge(first.next%2C%20second)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20first.next.prev%20%3D%20first%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20first.prev%20%3D%20null%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20first%3B%0A%20%20%20%20%20%20%20%20%7D%20else%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20second.next%20%3D%20merge(first%2C%20second.next)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20second.next.prev%20%3D%20second%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20second.prev%20%3D%20null%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20second%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%0A%20%20%20%0A%20%20%20%20public%20static%20void%20main(String%5B%5D%20args)%20%7B%0A%20%0A%20%20%20%20%20%20%20%20LinkedList%20list%20%3D%20new%20LinkedList()%3B%0A%20%20%20%20%20%20%20%20list.head%20%3D%20new%20Node(10)%3B%0A%20%20%20%20%20%20%20%20list.head.next%20%3D%20new%20Node(30)%3B%0A%20%20%20%20%20%20%20%20list.head.next.next%20%3D%20new%20Node(3)%3B%0A%20%20%20%20%20%20%20%20list.head.next.next.next%20%3D%20new%20Node(4)%3B%0A%20%20%20%20%20%20%20%20list.head.next.next.next.next%20%3D%20new%20Node(20)%3B%0A%20%20%20%20%20%20%20%20list.head.next.next.next.next.next%20%3D%20new%20Node(5)%3B%0A%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20Node%20node%20%3D%20null%3B%0A%20%20%20%20%20%20%20%20node%20%3D%20list.mergeSort(head)%3B%0A%20%20%20%20%20%20%20%20System.out.println(%22Linked%20list%20after%20sorting%20%3A%22)%3B%0A%20%20%20%20%20%20%20%20list.print(node)%3B%0A%20%0A%20%20%20%20%7D%0A%7D%0A%20\u2033 message=\u201dJava\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<h3 id=\"output\"><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/h3>\n<pre>Linked List after sorting\r\nForward Traversal using next pointer\r\n3 4 5 10 20 30\r\nBackward Traversal using prev pointer\r\n30 20 10 5 4 3<\/pre>\n<p><span style=\"color: #3366ff;\"><strong>Time Complexity: <\/strong><\/span>Time complexity of the above implementation is same as time complexity of MergeSort for <a href=\"https:\/\/www.wikitechy.com\/tutorials\/java\/arrays-in-java-example\" target=\"_blank\" rel=\"noopener\">arrays<\/a>. It takes<strong> \u0398(nLogn)<\/strong> time.<\/p>\n[ad type=\u201dbanner\u201d]\n","protected":false},"excerpt":{"rendered":"<p>JAVA Programming-Merge Sort for Doubly Linked List &#8211; Searching and Sorting &#8211; Merge sort for singly linked list is already discussed. The important change here is to modify the previous pointers also when merging two lists.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,2139,79476,83481,71670],"tags":[70250,72497,71272,70238,73367,70027,70028,70306,70233,73336,71386,70487,73368,70929,70889,70923,70877,71164,73326,70049,72405,72479,70262,71289,71268,71263,71714,71870,73340,70072,72489,73345,70652,71142,73338,70317,70022,70026,70544],"class_list":["post-25419","post","type-post","status-publish","format-standard","hentry","category-coding","category-java","category-linked-list","category-merge-sort","category-searching-and-sorting","tag-algorithm-complexity","tag-algorithm-for-linked-list","tag-algorithm-for-merge-sort","tag-algorithms-and-data-structures","tag-application-of-linked-list","tag-complexity-of-algorithm","tag-complexity-of-binary-search","tag-complexity-of-binary-search-algorithm","tag-data-structures-and-algorithms","tag-data-structures-and-algorithms-in-java","tag-data-structures-and-algorithms-in-java-tutorial","tag-doubly-linked-list","tag-doubly-linked-list-program-in-c","tag-heap-data-structure","tag-heap-in-data-structure","tag-heap-sort-algorithm-in-c","tag-heap-sort-time-complexity","tag-insertion-sort-algorithm-in-data-structure","tag-java-data-structures-and-algorithms","tag-linked-list","tag-linked-list-algorithm","tag-linked-list-in-data-structure","tag-merge-sort","tag-merge-sort-algorithm-in-c","tag-merge-sort-in-java","tag-merge-sort-java","tag-quicksort-algorithm-in-data-structure","tag-radix-sort-algorithm-in-data-structure","tag-reverse-a-linked-list","tag-searching-in-data-structure","tag-singly-linked-list-algorithm","tag-singly-linked-list-in-data-structure","tag-sorting-algorithms-in-c","tag-sorting-in-data-structure","tag-stack-in-data-structure","tag-the-complexity-of-binary-search-algorithm-is","tag-time-complexity","tag-time-complexity-of-binary-search","tag-time-complexity-of-merge-sort"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25419","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=25419"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25419\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=25419"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=25419"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=25419"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}