JavaScript Program for Quicksort On Singly Linked List


The Singly-linked list is a linear data structure that consists of nodes. Each node contains the data and the pointer to the next node which contains the memory address of the next node because the memory assigned to each node is not continuous.

Sorting is a technique by which we make all the elements of a particular data structure such as a linked list, array, vector, etc in a properly sorted manner in either increasing or decreasing order (if not specified in increasing order). We will see the proper code and the explanation in this article.

Introduction to Problem

In this problem, we have to implement the quicksort algorithm on the singly linked list. QuickSort is a kind of sorting algorithm or technique that is implemented using recursion and has the best time complexity of O(N * log(N)).

Recursion is a prerequisite to the quicksort algorithm. Recursion is a programming pattern in which we define a function that will keep calling itself with the inputs (that are passed as the parameters) keep decreasing or increasing in value. There will be a base condition present by which the recursive calls end. Let’s see the steps to implement the quicksort algorithm before implementing it in the code format.

Approach

To implement the QuickSort on the singly-linked list we are going to follow these steps −

  • To get a pivot node at the proper position, we will use the partition function.

    • The last element in the partition function is marked as a the pivot.

    • Then we will traverse the current list and relocate any node with a value larger than the pivot after the tail. If the node has a lower value, it should remain in its present or the current place.

    • At the end we will return the node that is the pivot.

  • We will find the linked list's tail node on the left side of the pivot and repeat for the left side of the linked list.

  • Similarly, following the left side, repeat for the linked list on the pivot's node right side.

  • As the entire linked list is now sorted, return the head of the linked list after connecting the left and right linked lists.

Example

// class to provide structure to the node 
class Node {
   constructor(data) {
      this.value = data;
      this.next = null;
   }
}

// defining the head of the linked list 
var head = null

// function to add a new node in a linked list 
function addNode(value) {
   var temp = new Node(value);
   if(head == null) {
   head = temp;
   }
   else{
      var current = head;
      while (current.next != null)    	{
         current = current.next;
      }
      current.next = temp;
   }
}

// function to print all the elements of the linked list 
function print(head) {
   var temp = head;
   var ll = "";
   while (temp.next != null) {
      ll += temp.value + " -> "
      temp = temp.next;
   }
   ll += temp.value + " -> null"; 
   console.log(ll)
}

// function for partition of the linked list 
function partitionLast(first , last) {
   if (first == last || first == null || last == null)	{
      return first
   }
   var prev_pivot = first;
   var cur= first;
   var pivot = last.value;
   
   // traversing one node before the last 
   
   // as end is the pivot 
   while (first != last) {
      if (first.value < pivot) {
         prev_pivot = cur;
         var temp = cur.value;
         cur.value = first.value;
         first.value = temp;
         cur = cur.next;
      }
      first = first.next;
   }
   
   // swapping the positions
   var temp = cur.value;
   cur.value = pivot;
   last.value = temp;
   
   // as current is now pivot so return previous one 
   return prev_pivot
}
function quickSort(first, last) {
   // base condition 
   if (first == null || first == last || first == last.next){
      return; 
   }
   
   // split list and partition recurse
   var prev_pivot = partitionLast(first, last);
   quickSort(first, prev_pivot);
   
   // if pivot is moved to the start make both of them same 
   
   // which means we have to pick the new pivot 
   if (prev_pivot != null && prev_pivot == first){
      quickSort(prev_pivot.next, last);
   }
   
   // if pivot is in between of the list,
   
   // start from next of pivot,
   
   // since we have pivot_prev, so we move two nodes
   else if (prev_pivot != null && prev_pivot.next != null)	{
      quickSort(prev_pivot.next.next, end);
   }
}

// creating the linked list 
addNode(30);
addNode(3);
addNode(4);
addNode(20);
addNode(5);
var end = head;
while (end.next != null) {
   end = end.next;
}
console.log("Linked List before sorting");
print(head);
quickSort(head, end);
console.log("Linked List after sorting");
print(head);

Time and Space Complexity

The time complexity of the above code is not constant and totally depends upon the given input, but on an average the time complexity of the above code is O(N*log(N)) and this is also the best case of the current sorting algorithm. Worst case of the quicksort algorithm is O(N*N), which is in-effecitve.

The space complexity of the above code is O(N) due to the recursion stack.

Conclusion

In this tutorial, we have implemented a JavaScript program for quicksort on a singly linked list. The Singly-linked list is a linear data structure that consists of nodes. QuickSort is a kind of sorting algorithm or technique that is implemented using recursion and has the best and average time complexity of O(N * log(N)) and Recursion is a prerequisite to the quicksort algorithm. The worst case for the time complexity of the above code is O(N*N).

Updated on: 14-Apr-2023

188 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements