sort linked list using bubble sort

void linked_list::sort ()
{
  int i,j=0;
  int counter=0;
  node *curr=head;
  node *trail=head;
  node *temp=NULL;

  while (curr !=NULL)
  {
    curr=curr->next;    //couting the number of items I have in my list. 
    counter++;          //this works fine.
  }

  curr=head->next;          // reseting the curr value for the 2nd position.

  for (i=0; i<counter; i++)
  {
    while (curr != NULL)
    {
      if (trail->data > curr->data)
      {
        temp=curr->next;      //bubble sort for the pointers.
        curr->next=trail;
        trail->next=temp;

        temp=curr;         //reseting trail and curr. curr gets back to be infront.
        curr=trail;     
        trail=temp;

        if (j==0)   //i'm using j to determine the start of the loop so i won't loose the head pointer.
        {
          head=trail;
        }

      }
      j++;
      trail=curr;
      curr=curr->next;   //traversing thru the list. nested loop.
    }

    trail=head;
    curr=trail->next;
    curr->next=trail->next->next;  //traversing thru the list. outer loop.
    j=0;
  }
}

3.8
5
ShadowCat7 70 points

                                    public void sortList() {  
        Node current = null, index = null;  
        int temp;  
        //Check whether list is empty  
        if(head == null) {  
            return;  
        }  
        else {  
            //Current will point to head  
            for(current = head; current.next != null; current = current.next) {  
                //Index will point to node next to current  
                for(index = current.next; index != null; index = index.next) {  
                    //If current's data is greater than index's data, swap the data of current and index  
                    if(current.data &gt; index.data) {  
                        temp = current.data;  
                        current.data = index.data;  
                        index.data = temp;  
                    }  
                }  
            }  
        }  
    }  

3.8 (5 Votes)
0
3.6
10
Jzacharuk 125 points

                                    public static void main(String[] args){
  LinkedList linkedlist = new LinkedList()

3.6 (10 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
can bubble sort be implemented in linked list bubble sort of linked list bubblesort linkedlist java implement bubble sort linkedlist in java bubble sort code in java linked list linked list with bubble sort c sort linked list using bubble sort how to perform bubble sort in stack with linked list in c implement bubble sort for linked list bubble sort in doubly linked list c++ single linked list java bubble sort bubble sort linked list c++ bubble sort in linked list in c Write the Bubble Sort algorithm using linked lists bubble sort using linked list bubble sort in linked list java bubble sort in linked list bubble sort doubly linked list bubble sort using linked list java Sort singly linked list using bubble sort python Sort singly linked list using bubble sort bubble sort on linked list bubble sort linked list java bubble sort for linked list bubble sort in a linked list bubble sort linked list c bubble sort on doubly linked list bubble sort using doubly linked list java bubble sort using doubly linked list bubble sort of Linked Lists sort doubly linked list c++ bubble bubble sort using singly linked list the bubble sort algorithm in linked list linked list bubble sort how to bubble sort a doubly linked list bubble sort in a linked list in C sorting in doubly linked list bubble sort doubly linked list java best sorting algorithm for doubly linked list selection sort doubly linked list in c quick sort linked list sorting through linked lists C bubble sort linked list how to sort linked list how to bubble sort in c linked list how to sort a linked list in java using bubble sort bubble sort java linked list singly linked list bubble sort bubble sort linked list bubble sort with linked list in c bubble sort with linked list bubble sort for linkedlist bubble sort doubly linked list c++ bubble sorting linked list algorithm sorting algorithms bubble sort linked list sort the doubley linked list in cpp bubble sort on a doubly linked list
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source