Search code examples
javasortinglinked-listbubble-sort

Why this code is not working correctly for sort a linkedlist using bubble sort?


I implemented bubble sort using LinkedList as follows. I am not able to find correct and efficient solution for this problem. What changes are needed in this code, to make workable with efficiency. If somebody has better and efficient implementation of bubble sort on linkedlist please provide it.

class SortList {
    int size;
    Node head;
    class Node{
    int data;

    Node next;
    Node(int data){
        this.data = data;
        this.next = null;
        }
    Node(){
        this.data = 0;
        this.next = null;
    }
    }

    public void push(int d) {
        Node newNode = new Node();

        newNode.data = d;

        newNode.next = head;

        head = newNode;
        size++;
    }
    public void display(){
    Node n = head;
    while(n!=null){
        System.out.print(n.data +" ");

        n = n.next;
        }
    }
    public int getLength(){
        int count=0;
        Node n = head;
        while(n!=null){
            count++;
            n = n.next;
            }
            return count;
    }
    public int getLengthR(Node n){

            if(n==null) return 0;
            return 1+getLengthR(n.next);

    }
    public int getL(){
    return getLengthR(head);
    }
    public static void main(String[] args) {
        SortList ls = new SortList();
    int[]arrList = {5,2,7,3,1,2};
    for(int i=0;i<arrList.length;i++){
        ls.push(arrList[i]);
        }
        ls.display();

        ls.sortList();

        ls.display();
    }

    public void sortList(){
    if(size > 1){
        Node node = head;
        Node nextNode = head.next;
            for(int i=0;i<size;i++){

            for(int j=0;j<size - i - 1;j++){
                while(node.data > nextNode.data){
                    Node temp =node;
                    node = nextNode;
                    nextNode = temp;
                }
                node = nextNode;
                nextNode = nextNode.next;
            }
        }

        }
    }
}

Solution

  • You should probably look at the StackOverFlow answer suggested in the comments. I modified your sort method using a slightly different tactic. Instead of swapping the nodes, I swapped the values in the nodes. This might not always be applicable, because there may be other data that is associated with the nodes that you're not using for sorting purposes that might also need to be swapped.

    Basically the approach below is to reduce the size of the list by one after each pass. This is done by keeping track of the node that was just put into its correct position using the variable terminal.

    public void sortList(){
        if(size > 1){
            Node terminal = null; 
            while (head.next != terminal) {
                Node node = head;
                Node nextNode = head.next;
    
                while (nextNode != terminal) {
                    if(node.data > nextNode.data){
                        int temp =node.data;
                        node.data = nextNode.data;
                        nextNode.data = temp;
                    }
                    node = nextNode;
                    nextNode = nextNode.next;
                }
                terminal = node;
            }
        }
    }