QuickSort on Doubly Linked List

Following is a typical recursive implementation of QuickSort for arrays. The implementation uses last element as pivot.

Doubly Linked List:

Doubly Linked List

[pastacode lang=”cpp” manual=”%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–%3E%20Array%20to%20be%20sorted%2C%20l%20%20–%3E%20Starting%20index%2C%20h%20%20–%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” message=”c++” highlight=”” provider=”manual”/]

Can we use same algorithm for Linked List?

Following is C++ implementation 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 sort the linked list using pointers to first and last nodes of linked list, similar to the above recursive function 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, quickSort() is just a wrapper function, the main recursive function is _quickSort() which is similar to quickSort() for array implementation.

[ad type=”banner”]

Implementation of C++ for QuickSort on Doubly Linked List:

[pastacode lang=”cpp” manual=”%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” message=”c++” highlight=”” provider=”manual”/]

Output :

Linked List before sorting
30  3  4  20  5
Linked List after sorting
3  4  5  20  30

Time Complexity: Time complexity of the above implementation is same as time complexity of QuickSort() for arrays. It takes O(n^2) time in worst case and O(nLogn) in average and best cases. The worst case occurs when the linked list is already sorted.

Can we implement random quick sort for linked list?

Quicksort 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.

[ad type=”banner”]

Tagged in:

, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,