QuickSort on Doubly Linked List is discussed here. QuickSort on Singly linked list was given as an exercise. Following is C++ implementation for same. The important things about implementation are, it changes pointers rather swapping data and time complexity is same as the implementation for Doubly Linked List.
In partition(), we consider last element as pivot. We traverse through the current list and if a node has value greater than pivot, we move it after tail. If the node has smaller value, we keep it at its current position.
In QuickSortRecur(), we first call partition() which places pivot at correct position and returns pivot. After pivot is placed at correct position, we find tail node of left side (list before pivot) and recur for left list. Finally, we recur for right list.
Linked List before sorting 30 3 4 20 5 Linked List after sorting 3 4 5 20 30
This article is contributed by Balasubramanian.N . Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Convert singly linked list into circular linked list
- Difference between Singly linked list and Doubly linked list
- Convert Singly Linked List to XOR Linked List
- QuickSort on Doubly Linked List
- Given only a pointer/reference to a node to be deleted in a singly linked list, how do you delete it?
- Alternating split of a given Singly Linked List | Set 1
- Reverse alternate K nodes in a Singly Linked List
- Select a Random Node from a Singly Linked List
- Circular Singly Linked List | Insertion
- Insertion Sort for Singly Linked List
- Recursive selection sort for singly linked list | Swapping node links
- Alternate Odd and Even Nodes in a Singly Linked List
- Find middle of singly linked list Recursively
- Binary Search on Singly Linked List
- C Program to reverse each node value in Singly Linked List
- Delete all Prime Nodes from a Singly Linked List
- Sum of the nodes of a Singly Linked List
- Sum and Product of the nodes of a Singly Linked List which are divisible by K
- Sum and Product of all Prime Nodes of a Singly Linked List
- Minimum and Maximum Prime Numbers of a Singly Linked List