|
| 1 | +/* |
| 2 | +Quick Sort on Linked List |
| 3 | +========================= |
| 4 | +
|
| 5 | +Sort the given Linked List using quicksort. which takes O(n^2) time in worst case and O(nLogn) in average and best cases, otherwise you may get TLE. |
| 6 | +
|
| 7 | +Input: |
| 8 | +In this problem, method takes 1 argument: address of the head of the linked list. The function should not read any input from stdin/console. |
| 9 | +The struct Node has a data part which stores the data and a next pointer which points to the next element of the linked list. |
| 10 | +There are multiple test cases. For each test case, this method will be called individually. |
| 11 | +
|
| 12 | +Output: |
| 13 | +Set *headRef to head of resultant linked list. |
| 14 | +
|
| 15 | +User Task: |
| 16 | +The task is to complete the function quickSort() which should set the *headRef to head of the resultant linked list. |
| 17 | +
|
| 18 | +Constraints: |
| 19 | +1<=T<=100 |
| 20 | +1<=N<=200 |
| 21 | +
|
| 22 | +Note: If you use "Test" or "Expected Output Button" use below example format |
| 23 | +
|
| 24 | +Example: |
| 25 | +Input: |
| 26 | +2 |
| 27 | +3 |
| 28 | +1 6 2 |
| 29 | +4 |
| 30 | +1 9 3 8 |
| 31 | +Output: |
| 32 | +1 2 6 |
| 33 | +1 3 8 9 |
| 34 | +
|
| 35 | +Explanation: |
| 36 | +Testcase 1: After sorting the nodes, we have 1, 2 and 6. |
| 37 | +Testcase 2: After sorting the nodes, we have 1, 3, 8 and 9. |
| 38 | +*/ |
| 39 | + |
| 40 | +node *getTail(node *curr) |
| 41 | +{ |
| 42 | + while (curr && curr->next) |
| 43 | + curr = curr->next; |
| 44 | + return curr; |
| 45 | +} |
| 46 | + |
| 47 | +node *partition(node *head, node *end, node **newHead, node **newEnd) |
| 48 | +{ |
| 49 | + node *pivot = end; |
| 50 | + node *prev = NULL, *cur = head, *tail = pivot; |
| 51 | + |
| 52 | + while (cur != pivot) |
| 53 | + { |
| 54 | + if (cur->data < pivot->data) |
| 55 | + { |
| 56 | + if ((*newHead) == NULL) |
| 57 | + (*newHead) = cur; |
| 58 | + |
| 59 | + prev = cur; |
| 60 | + cur = cur->next; |
| 61 | + } |
| 62 | + else |
| 63 | + { |
| 64 | + if (prev) |
| 65 | + prev->next = cur->next; |
| 66 | + node *tmp = cur->next; |
| 67 | + |
| 68 | + cur->next = NULL; |
| 69 | + tail->next = cur; |
| 70 | + tail = cur; |
| 71 | + cur = tmp; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + if ((*newHead) == NULL) |
| 76 | + (*newHead) = pivot; |
| 77 | + (*newEnd) = tail; |
| 78 | + return pivot; |
| 79 | +} |
| 80 | + |
| 81 | +node *helper(node *head, node *tail) |
| 82 | +{ |
| 83 | + if (!head || head == tail) |
| 84 | + return head; |
| 85 | + |
| 86 | + node *newHead = NULL, *newTail = NULL; |
| 87 | + auto pivot = partition(head, tail, &newHead, &newTail); |
| 88 | + if (newHead != pivot) |
| 89 | + { |
| 90 | + auto tmp = newHead; |
| 91 | + while (tmp->next != pivot) |
| 92 | + tmp = tmp->next; |
| 93 | + tmp->next = NULL; |
| 94 | + |
| 95 | + newHead = helper(newHead, tmp); |
| 96 | + |
| 97 | + tmp = getTail(newHead); |
| 98 | + tmp->next = pivot; |
| 99 | + } |
| 100 | + |
| 101 | + pivot->next = helper(pivot->next, newTail); |
| 102 | + return newHead; |
| 103 | +} |
| 104 | + |
| 105 | +void quickSort(struct node **headRef) |
| 106 | +{ |
| 107 | + *headRef = helper(*headRef, getTail(*headRef)); |
| 108 | +} |
0 commit comments