Reputation: 308
I know that quick sort can't be applied on singly linked list directly.
Some modifications are needed in quick sort to make it compatible with a singly linked list.
But I am not getting how to do this.
Note:- I don't want to swap the value of one node with another node. Indeed I want to sort the linked list by changing the next pointer of required nodes.
So please elaborate, how this can be done.
Upvotes: 1
Views: 523
Reputation: 350137
Here is how you could do it:
Here is an implementation in JavaScript:
class Node {
constructor(value, next=null) {
this.value = value;
this.next = next;
}
}
function quickSort(head, tail) {
if (tail == null || head == null || head == tail) return head;
let pivot = tail;
let curr = head;
let prev = null;
while (curr != pivot) {
let next = curr.next;
if (curr.value > pivot.value) {
// Move node after tail
if (prev == null) {
head = next;
} else {
prev.next = next;
}
curr.next = tail.next;
tail.next = curr;
tail = curr;
} else {
prev = curr;
}
curr = next;
}
// Sort right and left sublists with recursion
if (pivot != tail) pivot.next = quickSort(pivot.next, tail);
return quickSort(head, prev);
}
// Some helper/utility functions
function getTail(head) {
if (head == null) return null;
while (head.next != null) {
head = head.next;
}
return head;
}
function createList(arr) {
let head = null;
for (let i = arr.length - 1; i >= 0; i--) {
head = new Node(arr[i], head);
}
return head;
}
function toArray(head) {
let arr = [];
while (head != null) {
arr.push(head.value);
head = head.next;
}
return arr;
}
// Demo:
// Create list from array
let head = createList([4,2,6,1,5,3]);
// Apply quick sort
let tail = getTail(head);
head = quickSort(head, tail);
// Print
console.log(toArray(head));
Upvotes: 2