Open In App

Javascript Program For QuickSort On Singly Linked List

Last Updated : 03 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

QuickSort on Doubly Linked List is discussed here. QuickSort on Singly linked list was given as an exercise. The important things about implementation are, it changes pointers rather swapping data and time complexity is same as the implementation for Doubly Linked List.

sorting image

In partition(), we consider last element as pivot. We traverse through the current list and if a node has value greater than pivot, we move it after tail. If the node has smaller value, we keep it at its current position. 

In QuickSortRecur(), we first call partition() which places pivot at correct position and returns pivot. After pivot is placed at correct position, we find tail node of left side (list before pivot) and recur for left list. Finally, we recur for right list.

Javascript




<script>
// Javascript program for Quick Sort on
// Singly LinKed List
 
// Sort a linked list using quick sort
class Node
{
    constructor(val)
    {
        this.data = val;
        this.next = null;
    }
}
 
var head;
 
function addNode(data)
{
    if (head == null)
    {
        head = new Node(data);
        return;
    }
 
    var curr = head;
    while (curr.next != null)
        curr = curr.next;
 
    var newNode = new Node(data);
    curr.next = newNode;
}
 
function printList(n)
{
    while (n != null)
    {
        document.write(n.data);
        document.write(" ");
        n = n.next;
    }
}
 
// Takes first and last node,
// but do not break any links in
// the whole linked list
function partitionLast(start, end)
{
    if (start == end ||
        start == null ||
        end == null)
        return start;
 
    var pivot_prev = start;
    var curr = start;
    var pivot = end.data;
 
    // Iterate till one before the end,
    // no need to iterate till the end
    // because end is pivot
    while (start != end)
    {
        if (start.data < pivot)
        {
            // Keep tracks of last modified
            // item
            pivot_prev = curr;
            var temp = curr.data;
            curr.data = start.data;
            start.data = temp;
            curr = curr.next;
        }
        start = start.next;
    }
 
    // Swap the position of curr i.e.
    // next suitable index and pivot
    var temp = curr.data;
    curr.data = pivot;
    end.data = temp;
 
    // Return one previous to current
    // because current is now pointing
    // to pivot
    return pivot_prev;
}
 
function sort(start, end)
{
    if (start == null ||
        start == end ||
        start == end.next)
            return;
 
    // Split list and partition recurse
    var pivot_prev = partitionLast(start, end);
    sort(start, pivot_prev);
 
    // If pivot is picked and moved to the start,
    // that means start and pivot is same
    // so pick from next of pivot
    if (pivot_prev != null &&
        pivot_prev == start)
        sort(pivot_prev.next, end);
 
    // 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 (pivot_prev != null &&
             pivot_prev.next != null)
        sort(pivot_prev.next.next, end);
}
 
// Driver Code
addNode(30);
addNode(3);
addNode(4);
addNode(20);
addNode(5);
 
var n = head;
while (n.next != null)
    n = n.next;
 
document.write(
         "Linked List before sorting<br/>");
printList(head);
 
sort(head, n);
 
document.write(
         "<br/>Linked List after sorting<br/>");
printList(head);
// This code is contributed by umadevi9616
</script>


Output:

Linked List before sorting 
30 3 4 20 5 
Linked List after sorting 
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.

Please refer complete article on QuickSort on Singly Linked List for more details!



Previous Article
Next Article

Similar Reads

Java Program For QuickSort On Singly Linked List
QuickSort on Doubly Linked List is discussed here. QuickSort on Singly linked list was given as an exercise. The important things about implementation are, it changes pointers rather swapping data and time complexity is same as the implementation for Doubly Linked List. Recommended: Please solve it on "PRACTICE" first, before moving on to the solut
3 min read
C++ Program For QuickSort On Singly Linked List
QuickSort on Doubly Linked List is discussed here. QuickSort on Singly linked list was given as an exercise. The important things about implementation are, it changes pointers rather swapping data and time complexity is same as the implementation for Doubly Linked List. Recommended: Please solve it on "PRACTICE" first, before moving on to the solut
4 min read
Python Program For QuickSort On Singly Linked List
QuickSort on Doubly Linked List is discussed here. QuickSort on Singly linked list was given as an exercise. The important things about implementation are, it changes pointers rather swapping data and time complexity is same as the implementation for Doubly Linked List. Recommended: Please solve it on "PRACTICE" first, before moving on to the solut
3 min read
QuickSort on Singly Linked List
Given a linked list, apply the Quick sort algorithm to sort the linked list. The important things about implementation are, that it changes pointers rather than swapping data. Example: Recommended PracticeQuick Sort on Linked ListTry It! Follow the given steps to solve the problem: Call partition function to get a pivot node placed at its correct p
14 min read
Convert Singly Linked List into Circular Linked List using JavaScript
We are given a singly linked list and we need to convert it into a circular linked list, where the last node points back to the head of the list. Converting a singly linked list into a circular linked list involves making the last node's next pointer point back to the head, creating a circular structure. This transformation enables traversal from t
3 min read
Javascript Program For QuickSort On Doubly Linked List
Following is a typical recursive implementation of QuickSort for arrays. The implementation uses last element as pivot. C/C++ Code &lt;script&gt; /* A typical recursive implementation of Quicksort for array*/ /* This function takes last element as pivot, places the pivot element at its correct position in sorted array, and places all smaller (small
5 min read
Difference between Singly linked list and Doubly linked list
Introduction to Singly linked list : A singly linked list is a set of nodes where each node has two fields 'data' and 'link'. The 'data' field stores actual piece of information and 'link' field is used to point to next node. Basically the 'link' field stores the address of the next node. Introduction to Doubly linked list : A Doubly Linked List (D
2 min read
Convert Singly Linked List to XOR Linked List
Prerequisite: XOR Linked List – A Memory Efficient Doubly Linked List | Set 1XOR Linked List – A Memory Efficient Doubly Linked List | Set 2 An XOR linked list is a memory efficient doubly linked list in which the next pointer of every node stores the XOR of previous and next node's address. Given a singly linked list, the task is to convert the gi
9 min read
When is Doubly Linked List more Efficient than Singly Linked List?
Did you know there are some cases where a Doubly Linked List is more efficient than a Singly Linked List, even though it takes more memory compared to a Singly Linked List? What are those Cases? Well, we will discuss that in the following article, But first, let's talk about Singly and linked lists: What is a Singly Linked List?A singly linked list
4 min read
Convert singly linked list into circular linked list
Given a singly linked list, we have to convert it into circular linked list. For example, we have been given a singly linked list with four nodes and we want to convert this singly linked list into circular linked list. Approach: The idea is to traverse the singly linked list and check if the node is the last node or not. If the node is the last no
14 min read