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++
#include<iostream>usingnamespacestd;classNode{public:intdata;Node*next;Node(intx){data=x;next=nullptr;}};voidprintList(Node*curr){while(curr!=nullptr){cout<<curr->data<<" ";curr=curr->next;}cout<<endl;}// Returns the last node of the listNode*getTail(Node*cur){while(cur!=nullptr&&cur->next!=nullptr)cur=cur->next;returncur;}// Partitions the list taking the first element as the pivotNode*partition(Node*head,Node*tail){// Select the first node as the pivot nodeNode*pivot=head;// 'pre' and 'curr' are used to shift all // smaller nodes' data to the left side of the pivot nodeNode*pre=head;Node*curr=head;// Traverse the list until you reach the node after the tailwhile(curr!=tail->next){if(curr->data<pivot->data){swap(curr->data,pre->next->data);// Move 'pre' to the next nodepre=pre->next;}// Move 'curr' to the next nodecurr=curr->next;}swap(pivot->data,pre->data);// Return 'pre' as the new pivotreturnpre;}// Helper function for quick sortvoidquickSortHelper(Node*head,Node*tail){// Base case: if the list is empty or consists of a single nodeif(head==nullptr||head==tail){return;}// Call partition to find the pivot nodeNode*pivot=partition(head,tail);// Recursive call for the left part of the list (before the pivot)quickSortHelper(head,pivot);// Recursive call for the right part of the list (after the pivot)quickSortHelper(pivot->next,tail);}// The main function for quick sort. This is a wrapper over quickSortHelperNode*quickSort(Node*head){Node*tail=getTail(head);// Call the helper function to sort the listquickSortHelper(head,tail);returnhead;}intmain(){// Creating a linked list: 30 -> 3 -> 4 -> 20 -> 5Node*head=newNode(30);head->next=newNode(3);head->next->next=newNode(4);head->next->next->next=newNode(20);head->next->next->next->next=newNode(5);head=quickSort(head);printList(head);return0;}
C
#include<stdio.h>#include<stdlib.h>structNode{intdata;structNode*next;};voidprintList(structNode*curr){while(curr!=NULL){printf("%d ",curr->data);curr=curr->next;}printf("\n");}// Returns the last node of the liststructNode*getTail(structNode*cur){while(cur!=NULL&&cur->next!=NULL)cur=cur->next;returncur;}// Partitions the list taking the first element as the pivotstructNode*partition(structNode*head,structNode*tail){// Select the first node as the pivot nodestructNode*pivot=head;// 'pre' and 'curr' are used to shift all // smaller nodes' data to the left side of the pivot nodestructNode*pre=head;structNode*curr=head;// Traverse the list until you reach the node after the tailwhile(curr!=tail->next){// If current node's data is less than the pivot's dataif(curr->data<pivot->data){// Swap the data between 'curr' and 'pre->next'inttemp=curr->data;curr->data=pre->next->data;pre->next->data=temp;// Move 'pre' to the next nodepre=pre->next;}// Move 'curr' to the next nodecurr=curr->next;}// Swap the pivot's data with 'pre' dataintcurrData=pivot->data;pivot->data=pre->data;pre->data=currData;// Return 'pre' as the new pivotreturnpre;}// Helper function for quick sortvoidquickSortHelper(structNode*head,structNode*tail){// Base case: if the list is empty or consists of a single nodeif(head==NULL||head==tail){return;}// Call partition to find the pivot nodestructNode*pivot=partition(head,tail);// Recursive call for the left part // of the list (before the pivot)quickSortHelper(head,pivot);// Recursive call for the right part // of the list (after the pivot)quickSortHelper(pivot->next,tail);}// The main function for quick sort. This is a// wrapper over quickSortHelperstructNode*quickSort(structNode*head){// Find the tail of the liststructNode*tail=getTail(head);// Call the helper function to sort the listquickSortHelper(head,tail);returnhead;}structNode*createNode(intx){structNode*newNode=(structNode*)malloc(sizeof(structNode));newNode->data=x;newNode->next=NULL;returnnewNode;}intmain(){// Creating a linked list: 30 -> 3 -> 4 -> 20 -> 5structNode*head=createNode(30);head->next=createNode(3);head->next->next=createNode(4);head->next->next->next=createNode(20);head->next->next->next->next=createNode(5);head=quickSort(head);printList(head);return0;}
Java
classNode{intdata;Nodenext;Node(intx){data=x;next=null;}}classGfG{staticvoidprintList(Nodecurr){while(curr!=null){System.out.print(curr.data+" ");curr=curr.next;}System.out.println();}// Returns the last node of the liststaticNodegetTail(Nodecur){while(cur!=null&&cur.next!=null)cur=cur.next;returncur;}// Partitions the list taking the first element as the pivotstaticNodepartition(Nodehead,Nodetail){// Select the first node as the pivot nodeNodepivot=head;// 'pre' and 'curr' are used to shift all // smaller nodes' data to the left side of the pivot nodeNodepre=head;Nodecurr=head;// Traverse the list until you reach the node after the tailwhile(curr!=tail.next){// If current node's data is less than the pivot's dataif(curr.data<pivot.data){inttemp=curr.data;curr.data=pre.next.data;pre.next.data=temp;// Move 'pre' to the next nodepre=pre.next;}// Move 'curr' to the next nodecurr=curr.next;}// Swap the pivot's data with 'pre' dataintcurrData=pivot.data;pivot.data=pre.data;pre.data=currData;// Return 'pre' as the new pivotreturnpre;}// Helper function for quick sortstaticvoidquickSortHelper(Nodehead,Nodetail){// Base case: if the list is empty or consists of a single nodeif(head==null||head==tail){return;}// Call partition to find the pivot nodeNodepivot=partition(head,tail);// Recursive call for the left part of // the list (before the pivot)quickSortHelper(head,pivot);// Recursive call for the right part of // the list (after the pivot)quickSortHelper(pivot.next,tail);}// The main function for quick sort. // This is a wrapper over quickSortHelperstaticNodequickSort(Nodehead){// Find the tail of the listNodetail=getTail(head);// Call the helper function to sort the listquickSortHelper(head,tail);returnhead;}publicstaticvoidmain(String[]args){// Creating a linked list: 30 -> 3 -> 4 -> 20 -> 5Nodehead=newNode(30);head.next=newNode(3);head.next.next=newNode(4);head.next.next.next=newNode(20);head.next.next.next.next=newNode(5);head=quickSort(head);printList(head);}}
Python
classNode:def__init__(self,x):self.data=xself.next=NonedefprintList(curr):whilecurr:print(curr.data,end=" ")curr=curr.nextprint()# Returns the last node of the listdefgetTail(cur):whilecurandcur.next:cur=cur.nextreturncur# Partitions the list taking the first element as the pivotdefpartition(head,tail):# Select the first node as the pivot nodepivot=head# 'pre' and 'curr' are used to shift all# smaller nodes' data to the left side of the pivot nodepre=headcurr=head# Traverse the list until you reach the node after the tailwhilecurr!=tail.next:# If current node's data is less than the pivot's dataifcurr.data<pivot.data:# Swap the data between 'curr' and 'pre.next'curr.data,pre.next.data=pre.next.data,curr.datapre=pre.nextcurr=curr.next# Swap the pivot's data with 'pre' datapivot.data,pre.data=pre.data,pivot.data# Return 'pre' as the new pivotreturnpredefquickSortHelper(head,tail):# Base case: if the list is empty or consists of a single nodeifheadisNoneorhead==tail:return# Call partition to find the pivot nodepivot=partition(head,tail)# Recursive call for the left part of the list (before the pivot)quickSortHelper(head,pivot)# Recursive call for the right part of the list (after the pivot)quickSortHelper(pivot.next,tail)# The main function for quick sort.# This is a wrapper over quickSortHelperdefquickSort(head):# Find the tail of the listtail=getTail(head)# Call the helper function to sort the listquickSortHelper(head,tail)returnheadif__name__=="__main__":# Creating a linked list: 30 -> 3 -> 4 -> 20 -> 5head=Node(30)head.next=Node(3)head.next.next=Node(4)head.next.next.next=Node(20)head.next.next.next.next=Node(5)head=quickSort(head)printList(head)
C#
usingSystem;classNode{publicintData;publicNodenext;publicNode(intx){Data=x;next=null;}}classGfG{staticvoidPrintList(Nodecurr){while(curr!=null){Console.Write(curr.Data+" ");curr=curr.next;}Console.WriteLine();}// Returns the last node of the liststaticNodeGetTail(Nodecur){while(cur!=null&&cur.next!=null)cur=cur.next;returncur;}// Partitions the list taking the first element as the pivotstaticNodePartition(Nodehead,Nodetail){// Select the first node as the pivot nodeNodepivot=head;// 'pre' and 'curr' are used to shift all// smaller nodes' data to the left side of the pivot nodeNodepre=head;Nodecurr=head;// Traverse the list until you reach the node after the tailwhile(curr!=tail.next){// If current node's data is less than the pivot's dataif(curr.Data<pivot.Data){// Swap the data between 'curr' and 'pre->Next'inttemp=curr.Data;curr.Data=pre.next.Data;pre.next.Data=temp;// Move 'pre' to the next nodepre=pre.next;}// Move 'curr' to the next nodecurr=curr.next;}// Swap the pivot's data with 'pre' dataintcurrData=pivot.Data;pivot.Data=pre.Data;pre.Data=currData;// Return 'pre' as the new pivotreturnpre;}// Helper function for quick sortstaticvoidQuickSortHelper(Nodehead,Nodetail){// Base case: if the list is empty or consists of a single nodeif(head==null||head==tail){return;}// Call partition to find the pivot nodeNodepivot=Partition(head,tail);// Recursive call for the left // part of the list (before the pivot)QuickSortHelper(head,pivot);// Recursive call for the right part // of the list (after the pivot)QuickSortHelper(pivot.next,tail);}// The main function for quick sort. // This is a wrapper over QuickSortHelperstaticNodeQuickSort(Nodehead){// Find the tail of the listNodetail=GetTail(head);// Call the helper function to sort the listQuickSortHelper(head,tail);returnhead;}staticvoidMain(){// Creating a linked list: 30 -> 3 -> 4 -> 20 -> 5Nodehead=newNode(30);head.next=newNode(3);head.next.next=newNode(4);head.next.next.next=newNode(20);head.next.next.next.next=newNode(5);head=QuickSort(head);PrintList(head);}}
JavaScript
classNode{constructor(x){this.data=x;this.next=null;}}functionprintList(curr){while(curr){console.log(curr.data+" ");curr=curr.next;}console.log();}// Returns the last node of the listfunctiongetTail(cur){while(cur&&cur.next){cur=cur.next;}returncur;}// Partitions the list taking the first element as the pivotfunctionpartition(head,tail){// Select the first node as the pivot nodeletpivot=head;// 'pre' and 'curr' are used to shift all // smaller nodes' data to the left side of the pivot nodeletpre=head;letcurr=head;// Traverse the list until you reach the node after the tailwhile(curr!==tail.next){// If current node's data is less than the pivot's dataif(curr.data<pivot.data){// Swap the data between 'curr' and 'pre.next'[curr.data,pre.next.data]=[pre.next.data,curr.data];// Move 'pre' to the next nodepre=pre.next;}// Move 'curr' to the next nodecurr=curr.next;}// Swap the pivot's data with 'pre' data[pivot.data,pre.data]=[pre.data,pivot.data];// Return 'pre' as the new pivotreturnpre;}// Helper function for quick sortfunctionquickSortHelper(head,tail){// Base case: if the list is empty or // consists of a single nodeif(!head||head===tail){return;}// Call partition to find the pivot nodeletpivot=partition(head,tail);// Recursive call for the left part // of the list (before the pivot)quickSortHelper(head,pivot);// Recursive call for the right part // of the list (after the pivot)quickSortHelper(pivot.next,tail);}// The main function for quick sort. // This is a wrapper over quickSortHelperfunctionquickSort(head){// Find the tail of the listlettail=getTail(head);// Call the helper function to sort the listquickSortHelper(head,tail);returnhead;}// Creating a linked list: 30 -> 3 -> 4 -> 20 -> 5lethead=newNode(30);head.next=newNode(3);head.next.next=newNode(4);head.next.next.next=newNode(20);head.next.next.next.next=newNode(5);head=quickSort(head);printList(head);
Output
3 4 5 20 30
Time Complexity: O(n * log n), It takes O(n2) time in the worst case and O(n log n) in the average or best case.