Selecting a pivot from the linked list, typically the last node.
The linked list is then partitioned such that all elements smaller than the pivot are placed on the left, while those greater than the pivot are placed on the right.
Once the partitioning is complete, the algorithm recursively applies the same process to the left and right linked lists.
The sorted left list, pivot and right list are combined to get the complete sorted list.
C++
// C++ program to sort a doubly linked list // using quicksort#include<iostream>usingnamespacestd;classNode{public:intdata;Node*next;Node*prev;Node(intx){data=x;next=nullptr;prev=nullptr;}};// Function to swap the data of two nodesvoidswap(Node*a,Node*b){// Swap the data in the nodesinttemp=a->data;a->data=b->data;b->data=temp;}// Function to partition the list and find pivotNode*partition(Node*low,Node*high){// Set pivot to the high nodeintpivot=high->data;// Pointer to place smaller elementsNode*i=low->prev;// Traverse the list to rearrange nodesfor(Node*j=low;j!=high;j=j->next){// If current node's data is less than or // equal to the pivotif(j->data<=pivot){// Move i forward and swap with ji=(i==nullptr)?low:i->next;swap(i,j);}}// Move i to the correct pivot positioni=(i==nullptr)?low:i->next;// Swap pivot with i's dataswap(i,high);returni;}// Recursive function to apply quicksortvoidquickSort(Node*low,Node*high){// Base case: if the list has one element or // invalid rangeif(low!=nullptr&&high!=nullptr&&low!=high&&low!=high->next){// Find the partition node (pivot)Node*pivot=partition(low,high);// Recursively sort the left halfquickSort(low,pivot->prev);// Recursively sort the right halfquickSort(pivot->next,high);}}// Function to get the last node of the listNode*getLastNode(Node*head){// Traverse to the end of the listwhile(head!=nullptr&&head->next!=nullptr){head=head->next;}returnhead;}voidprintList(Node*node){Node*curr=node;while(curr!=nullptr){cout<<" "<<curr->data;curr=curr->next;}}intmain(){// Create a hard-coded doubly linked list:// 5 <-> 3 <-> 4 <-> 1 <-> 2Node*head=newNode(5);head->next=newNode(3);head->next->prev=head;head->next->next=newNode(4);head->next->next->prev=head->next;head->next->next->next=newNode(1);head->next->next->next->prev=head->next->next;head->next->next->next->next=newNode(2);head->next->next->next->next->prev=head->next->next->next;Node*last=getLastNode(head);quickSort(head,last);printList(head);return0;}
C
// C program to sort a doubly linked list// using quicksort#include<stdio.h>#include<stdlib.h>structNode{intdata;structNode*next;structNode*prev;};// Function to swap the data of two nodesvoidswap(structNode*a,structNode*b){// Swap the data in the nodesinttemp=a->data;a->data=b->data;b->data=temp;}// Function to partition the list and find pivotstructNode*partition(structNode*low,structNode*high){// Set pivot to the high nodeintpivot=high->data;// Pointer to place smaller elementsstructNode*i=low->prev;// Traverse the list to rearrange nodesfor(structNode*j=low;j!=high;j=j->next){// If current node's data is less than // or equal to the pivotif(j->data<=pivot){// Move `i` forward and swap with `j`i=(i==NULL)?low:i->next;swap(i,j);}}// Move `i` to the correct pivot positioni=(i==NULL)?low:i->next;// Swap pivot with `i`'s dataswap(i,high);returni;}// Recursive function to apply quicksortvoidquickSort(structNode*low,structNode*high){// Base case: if the list has one element or // invalid rangeif(low!=NULL&&high!=NULL&&low!=high&&low!=high->next){// Find the partition node (pivot)structNode*pivot=partition(low,high);// Recursively sort the left halfquickSort(low,pivot->prev);// Recursively sort the right halfquickSort(pivot->next,high);}}// Function to get the last node of the liststructNode*getLastNode(structNode*head){// Traverse to the end of the listwhile(head!=NULL&&head->next!=NULL){head=head->next;}returnhead;}voidprintList(structNode*node){structNode*curr=node;while(curr!=NULL){printf("%d ",curr->data);curr=curr->next;}}structNode*createNode(intnew_data){structNode*new_node=(structNode*)malloc(sizeof(structNode));new_node->data=new_data;new_node->next=NULL;new_node->prev=NULL;returnnew_node;}intmain(){// Create a hard-coded doubly linked list:// 5 <-> 3 <-> 4 <-> 1 <-> 2structNode*head=createNode(5);head->next=createNode(3);head->next->prev=head;head->next->next=createNode(4);head->next->next->prev=head->next;head->next->next->next=createNode(1);head->next->next->next->prev=head->next->next;head->next->next->next->next=createNode(2);head->next->next->next->next->prev=head->next->next->next;structNode*last=getLastNode(head);quickSort(head,last);printList(head);return0;}
Java
// Java program to sort a doubly linked list// using quicksortclassNode{intdata;Nodenext,prev;Node(intx){data=x;next=null;prev=null;}}publicclassGfG{// Function to swap data of two nodesstaticvoidswap(Nodea,Nodeb){// Swap data between `a` and `b`inttemp=a.data;a.data=b.data;b.data=temp;}// Function to partition the list around pivotstaticNodepartition(Nodelow,Nodehigh){// Set pivot to the data of `high` nodeintpivot=high.data;// Pointer to place smaller elementsNodei=low.prev;// Traverse list from `low` to `high`for(Nodej=low;j!=high;j=j.next){// If current data is <= pivotif(j.data<=pivot){// Move `i` forward and swap with `j`i=(i==null)?low:i.next;swap(i,j);}}// Move `i` to correct pivot positioni=(i==null)?low:i.next;// Swap pivot data with `i`'s dataswap(i,high);returni;}// Recursive quicksort functionstaticvoidquickSort(Nodelow,Nodehigh){// Base case: stop recursion when invalid rangeif(low!=null&&high!=null&&low!=high&&low!=high.next){// Partition the list and get the pivot nodeNodepivot=partition(low,high);// Recursively sort the left halfquickSort(low,pivot.prev);// Recursively sort the right halfquickSort(pivot.next,high);}}// Function to get the last node of the liststaticNodegetLastNode(Nodehead){// Traverse to the end of the listwhile(head!=null&&head.next!=null){head=head.next;}returnhead;}staticvoidprintList(Nodenode){Nodecurr=node;while(curr!=null){System.out.print(" "+curr.data);curr=curr.next;}}publicstaticvoidmain(String[]args){// Create a hard-coded doubly linked list:// 5 <-> 3 <-> 4 <-> 1 <-> 2Nodehead=newNode(5);head.next=newNode(3);head.next.prev=head;head.next.next=newNode(4);head.next.next.prev=head.next;head.next.next.next=newNode(1);head.next.next.next.prev=head.next.next;head.next.next.next.next=newNode(2);head.next.next.next.next.prev=head.next.next.next;Nodelast=getLastNode(head);quickSort(head,last);printList(head);}}
Python
# Python program to sort a doubly linked list# using quicksortclassNode:def__init__(self,data):self.data=dataself.next=Noneself.prev=None# Function to swap data between two nodesdefswap(a,b):# Swap the data between node `a` and node `b`a.data,b.data=b.data,a.data# Partition function for quicksortdefpartition(low,high):# Set pivot as the data of `high` nodepivot=high.data# Pointer to place smaller elementsi=low.prev# Traverse from `low` to `high`curr=lowwhilecurr!=high:# If current node's data is <= pivotifcurr.data<=pivot:# Move `i` forward and swap with `curr`i=lowifiisNoneelsei.nextswap(i,curr)curr=curr.next# Move `i` to the correct pivot positioni=lowifiisNoneelsei.next# Swap pivot data with `i`'s dataswap(i,high)returni# Recursive quicksort functiondefquick_sort(low,high):# Base case: stop when invalid rangeiflowandhighandlow!=highandlow!=high.next:# Partition the list and get the pivot nodepivot=partition(low,high)# Recursively sort the left halfquick_sort(low,pivot.prev)# Recursively sort the right halfquick_sort(pivot.next,high)# Function to get the last node of the listdefget_last_node(head):# Traverse to the last nodewhileheadandhead.next:head=head.nextreturnheaddefprint_list(node):curr=nodewhilecurr:print(curr.data,end=" ")curr=curr.nextif__name__=='__main__':# Create a hard-coded doubly linked list:# 5 <-> 3 <-> 4 <-> 1 <-> 2head=Node(5)head.next=Node(3)head.next.prev=headhead.next.next=Node(4)head.next.next.prev=head.nexthead.next.next.next=Node(1)head.next.next.next.prev=head.next.nexthead.next.next.next.next=Node(2)head.next.next.next.next.prev=head.next.next.nextlast_node=get_last_node(head)quick_sort(head,last_node)print_list(head)
C#
// C# program to sort a singly linked list // using quicksort usingSystem;publicclassNode{publicintdata;publicNodenext;publicNode(intnew_data){data=new_data;next=null;}}classGfG{// Function to swap data between two nodesstaticvoidSwap(Nodea,Nodeb){// Swap data between node `a` and node `b`inttemp=a.data;a.data=b.data;b.data=temp;}// Partition function for quicksortstaticNodePartition(Nodelow,Nodehigh){// Set pivot as the data of `high` nodeintpivot=high.data;// Pointer to place smaller elementsNodei=low;// Traverse from `low` to `high`Nodecurr=low;while(curr!=high){// If current node's data is <= pivotif(curr.data<=pivot){// Swap data between `i` and `curr`Swap(i,curr);// Move `i` forwardi=i.next;}curr=curr.next;}// Swap pivot data with `i`'s dataSwap(i,high);returni;}// Recursive quicksort functionstaticvoidQuickSort(Nodelow,Nodehigh){// Base case: stop when invalid rangeif(low!=high&&low!=null&&high!=null){// Partition the list and get the pivot nodeNodepivot=Partition(low,high);// Recursively sort the left halfNodebeforePivot=low;while(beforePivot!=null&&beforePivot.next!=pivot){beforePivot=beforePivot.next;}// Sort left of pivot only if existsif(beforePivot!=null&&beforePivot!=pivot)QuickSort(low,beforePivot);// Recursively sort the right halfif(pivot!=null&&pivot.next!=high)QuickSort(pivot.next,high);}}// Function to get the last node of the liststaticNodeGetLastNode(Nodehead){// Traverse the list to find the last nodewhile(head!=null&&head.next!=null){head=head.next;}returnhead;}staticvoidPrintList(Nodenode){Nodecurr=node;while(curr!=null){Console.Write(" "+curr.data);curr=curr.next;}}staticvoidMain(string[]args){// Create a hard-coded linked list:// 5 -> 3 -> 4 -> 1 -> 2Nodehead=newNode(5);head.next=newNode(3);head.next.next=newNode(4);head.next.next.next=newNode(1);head.next.next.next.next=newNode(2);NodelastNode=GetLastNode(head);QuickSort(head,lastNode);PrintList(head);}}
JavaScript
// JavaScript program to sort a doubly linked list// using quicksortclassNode{constructor(data){this.data=data;this.next=null;this.prev=null;}}// Function to swap the data between two nodesfunctionswap(a,b){lettemp=a.data;a.data=b.data;b.data=temp;}// Partition function for quicksortfunctionpartition(low,high){// Set pivot as the data of `high` nodeletpivot=high.data;// Pointer to place smaller elementsleti=low.prev;// Traverse from `low` to `high`for(letj=low;j!==high;j=j.next){if(j.data<=pivot){i=(i===null)?low:i.next;swap(i,j);}}// Swap pivot data with `i.next`'s datai=(i===null)?low:i.next;swap(i,high);returni;}// Recursive quicksort functionfunctionquickSort(low,high){if(low!==null&&high!==null&&low!==high&&low!==high.next){letpivot=partition(low,high);// Sort left side of the pivotquickSort(low,pivot.prev);// Sort right side of the pivotquickSort(pivot.next,high);}}// Function to get the last node of the listfunctiongetLastNode(head){while(head!==null&&head.next!==null){head=head.next;}returnhead;}functionprintList(node){letcurr=node;while(curr!==null){console.log(" "+curr.data);curr=curr.next;}}// Create a hard-coded doubly linked list:// 5 <-> 3 <-> 4 <-> 1 <-> 2lethead=newNode(5);head.next=newNode(3);head.next.prev=head;head.next.next=newNode(4);head.next.next.prev=head.next;head.next.next.next=newNode(1);head.next.next.next.prev=head.next.next;head.next.next.next.next=newNode(2);head.next.next.next.next.prev=head.next.next.next;letlastNode=getLastNode(head);quickSort(head,lastNode);printList(head);
Output
1 2 3 4 5
Time Complexity: On average, quicksort has a time complexity of O(nlogn), where n is the number of nodes. In the worst case (e.g., already sorted list), it becomes O(n²). Auxiliary Space: O(logn) due to the recursion stack in average cases, and O(n) in the worst case.