|
| 1 | +package com.thealgorithms.datastructures.lists; |
| 2 | +/* |
| 3 | + * |
| 4 | + * @aurthor - Prabhat-Kumar-42 |
| 5 | + * @github - https://github.com/Prabhat-Kumar-42 |
| 6 | + * |
| 7 | + * Problem : |
| 8 | + * QuickSort on Linked List |
| 9 | + * |
| 10 | + * Note: Taking head as pivot in current implementation. |
| 11 | + * N represents NULL node |
| 12 | + * Example: |
| 13 | + * |
| 14 | + * -> Given Linked List : |
| 15 | + * 5 -> 3 -> 8 -> 1 -> 10 -> 2 -> 7 -> 4 -> 9 -> 6 |
| 16 | + * |
| 17 | + * -> How Sorting will work according to the QuickSort Algo written below |
| 18 | + * |
| 19 | + * current pivot : 5 |
| 20 | + * List lessThanPivot : 3 -> 1 -> 2 -> 4 |
| 21 | + * List greaterThanPivot : 5 -> 8 -> 10 -> 7 -> 9 -> 6 |
| 22 | + * |
| 23 | + * -> reccur for lessThanPivot and greaterThanPivot |
| 24 | + * |
| 25 | + * lessThanPivot : |
| 26 | + * current pivot : 3 |
| 27 | + * lessThanPivot : 1 -> 2 |
| 28 | + * greaterThanPivot : 4 |
| 29 | + * |
| 30 | + * greaterThanPivot: |
| 31 | + * current pivot : 5 |
| 32 | + * lessThanPivot : null |
| 33 | + * greaterThanPivot : 8 -> 10 -> 7 -> 9 -> 6 |
| 34 | + * |
| 35 | + * By following the above pattern, reccuring tree will form like below : |
| 36 | + * |
| 37 | + * List-> 5 -> 3 -> 8 -> 1 -> 10 -> 2 -> 7 -> 4 -> 9 -> 6 |
| 38 | + * |
| 39 | + * Pivot : 5 |
| 40 | + * /\ |
| 41 | + * / \ |
| 42 | + * / \ |
| 43 | + * / \ |
| 44 | + * / \ |
| 45 | + * List: (3 -> 1 -> 2 -> 4) (5 -> 8 -> 10 -> 7 -> 9 -> 6) |
| 46 | + * Pivot : 3 5 |
| 47 | + * /\ /\ |
| 48 | + * / \ / \ |
| 49 | + * / \ / \ |
| 50 | + * / \ / \ |
| 51 | + * List: (1 -> 2) (4) (N) (8 -> 10 -> 7 -> 9 -> 6) |
| 52 | + * Pivot: 1 4 8 |
| 53 | + * /\ /\ /\ |
| 54 | + * / \ / \ / \ |
| 55 | + * / \ / \ / \ |
| 56 | + * List: (N) (2) (N) (N) (6 -> 7) (9 -> 10) |
| 57 | + * Pivot: 2 6 9 |
| 58 | + * /\ /\ /\ |
| 59 | + * / \ / \ / \ |
| 60 | + * / \ / \ / \ |
| 61 | + * List: (N) (N) (N) (7) (N) (10) |
| 62 | + * Pivot: 7 10 |
| 63 | + * /\ /\ |
| 64 | + * / \ / \ |
| 65 | + * / \ / \ |
| 66 | + * (N) (N) (N) (N) |
| 67 | + * |
| 68 | + * |
| 69 | + * -> After this the tree will reccur back (or backtrack) |
| 70 | + * and the returning list from left and right subtree will attach |
| 71 | + * themselves around pivot. |
| 72 | + * i.e. , |
| 73 | + * (listFromLeftSubTree) -> (Pivot) -> (listFromRightSubtree) |
| 74 | + * |
| 75 | + * This will continue until whole list is merged back |
| 76 | + * |
| 77 | + * eg : |
| 78 | + * Megring the above Tree back we get : |
| 79 | + * |
| 80 | + * List: (1 -> 2) (4) (6 -> 7) (9 -> 10) |
| 81 | + * \ / \ / |
| 82 | + * \ / \ / |
| 83 | + * \ / \ / |
| 84 | + * \ / \ / |
| 85 | + * \ / \ / |
| 86 | + * \ / \ / |
| 87 | + * \ / \ / |
| 88 | + * Pivot: 3 8 |
| 89 | + * List: (1 -> 2 -> 3 -> 4) (6 -> 7 -> 8 -> 9 -> 10) |
| 90 | + * \ / |
| 91 | + * \ / |
| 92 | + * \ / |
| 93 | + * \ / |
| 94 | + * \ / |
| 95 | + * \ / |
| 96 | + * \ / |
| 97 | + * \/ |
| 98 | + * Pivot: 5 |
| 99 | + * List: (1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 10) |
| 100 | + * |
| 101 | + * |
| 102 | + * -> This will result in a sorted Linked List |
| 103 | + */ |
| 104 | + |
| 105 | +public class QuickSortLinkedList { |
| 106 | + |
| 107 | + private SinglyLinkedList list = null; // Linked list |
| 108 | + private Node head = null; // head of the list |
| 109 | + // Counstructor |
| 110 | + public QuickSortLinkedList(SinglyLinkedList list) { |
| 111 | + this.list = list; |
| 112 | + this.head = list.getHead(); |
| 113 | + } |
| 114 | + |
| 115 | + // Function to sort a linked list using the Quick Sort algorithm |
| 116 | + public void sortList() { |
| 117 | + head = sortList(head); |
| 118 | + list.setHead(head); |
| 119 | + } |
| 120 | + // helper function to apply QuickSort to the stored list |
| 121 | + public Node sortList(Node head) { |
| 122 | + if (head == null || head.next == null) { |
| 123 | + return head; |
| 124 | + } |
| 125 | + |
| 126 | + // Choose the first element as the pivot |
| 127 | + Node pivot = head; |
| 128 | + head = head.next; |
| 129 | + pivot.next = null; |
| 130 | + |
| 131 | + Node lessHead = new Node(); // stores the nodes cantaining data less than pivot node |
| 132 | + Node lessTail = lessHead; // tail of lessHead |
| 133 | + Node greaterHead = new Node(); // stores the nodes cantaining data greater than pivot node |
| 134 | + Node greaterTail = greaterHead; // tail of greaterHead |
| 135 | + |
| 136 | + // Partition the list around the pivot |
| 137 | + while (head != null) { |
| 138 | + if (head.value < pivot.value) { |
| 139 | + lessTail.next = head; |
| 140 | + lessTail = lessTail.next; |
| 141 | + } else { |
| 142 | + greaterTail.next = head; |
| 143 | + greaterTail = greaterTail.next; |
| 144 | + } |
| 145 | + head = head.next; |
| 146 | + } |
| 147 | + |
| 148 | + // Seperating lessHead and greaterHead to form two seperate linkedList |
| 149 | + lessTail.next = null; |
| 150 | + greaterTail.next = null; |
| 151 | + |
| 152 | + // Recursively sort the sublists |
| 153 | + Node sortedLess = sortList(lessHead.next); |
| 154 | + Node sortedGreater = sortList(greaterHead.next); |
| 155 | + |
| 156 | + // Combine the sorted sublists and pivot |
| 157 | + if (sortedLess == null) { |
| 158 | + pivot.next = sortedGreater; |
| 159 | + return pivot; |
| 160 | + } else { |
| 161 | + Node current = sortedLess; |
| 162 | + while (current.next != null) { |
| 163 | + current = current.next; |
| 164 | + } |
| 165 | + current.next = pivot; |
| 166 | + pivot.next = sortedGreater; |
| 167 | + return sortedLess; |
| 168 | + } |
| 169 | + } |
| 170 | +} |
0 commit comments