Skip to main content

Bubble sort on linked list in typescript

Bubble sort in linked list

Ts program for Bubble sort on linked list.

// Typescript program for
// Bubble Sort For Linked List
class Node
{
    public data:number;
    public next:Node;
    constructor(data:number)
    {
        this.data = data;
        this.next = null;
    }
}
class LinkedList
{
    public head:Node;
    // Class constructors
    constructor()
    {
        this.head = null;
    }
    // Add node at the beginning of linked list
    public  insert(value:number)
    {
        // Create a new node
        var node = new Node(value);
        // Add node at front
        node.next = this.head;
        // Make new head
        this.head = node;
    }
    // Display all elements
    public  display()
    {
        if (this.head != null)
        {
            var temp = this.head;
            while (temp != null)
            {
                // Display node value
                console.log("  " + temp.data);
                // Visit to next node
                temp = temp.next;
            }
        }
        else
        {
            console.log("Empty Linked list");
        }
    }
    // Perform bubble sort in single linked list
    public  bubbleSort()
    {
        if (this.head != null)
        {
            var current:Node = null;
            var status = false;
            do
            {
                // Start with first node
                current = this.head;
                // Reset working status
                status = false;
                while (current != null && current.next != null)
                {
                    if (current.data > current.next.data)
                    {
                        // Swap node values
                        current.data = current.data + current.next.data;
                        current.next.data = current.data - current.next.data;
                        current.data = current.data - current.next.data;
                        // When node value change
                        status = true;
                    }
                    // Visit to next node
                    current = current.next;
                }
            } while (status);
        }
        else
        {
            console.log("Empty Linked list");
        }
    }
    public static  main()
    {
        var task = new LinkedList();
        // Insert element of linked list
        task.insert(15);
        task.insert(5);
        task.insert(42);
        task.insert(9);
        task.insert(50);
        task.insert(7);
        console.log(" Before sort : ");
        // Display all node
        task.display();
        task.bubbleSort();
        console.log("\n After sort  : ");
        task.display();
    }
}
LinkedList.main();
/*
 file : code.ts
 tsc --target es6 code.ts
 node code.js
 */

Output

 Before sort :
  7
  50
  9
  42
  5
  15

 After sort  :
  5
  7
  9
  15
  42
  50




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment