Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8883281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:46:35+00:00 2026-06-14T20:46:35+00:00

I am trying to sort a singly linked list using bubble sort. If there

  • 0

I am trying to sort a singly linked list using bubble sort. If there is a simple mistake then please pardon. Please tell me where I am going wrong. Program stops unexpectedly when I try to do this.

void sortBubble()
{
      Node *i=start,*j=start;Node *temp;Node* prev=start;Node* agla;
      while(i->next!=NULL)
      {
           cout<<"\nhello 1";
           j=i;
           agla=j->next;
           while(agla!=NULL)
           {
                temp=NULL;temp->next=NULL;
                cout<<"\nhello2";

                if(*(j) > *(agla))
                {
                     temp=agla->next;
                     agla->next=j;
                     prev->next=agla;
                     prev=agla;
                     agla=j->next;
                     j->next=temp;
                }
                else{
                     prev=j;
                     j=agla;
                     agla=j->next;}
                }
                prev=i;
                i=i->next;
           }
      }
 }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T20:46:36+00:00Added an answer on June 14, 2026 at 8:46 pm

    Your first obvious mistake that absolutely leads to program crash is:

           while(agla!=NULL)
           {
                temp=NULL;temp->next=NULL;
    

    You are setting a variable to NULL, then setting its fields. A null pointer points to nowhere, so you cannot edit its contents.

    Remove temp->next=NULL;


    Edit:

    Your program logic is not correct. You ruin the list after a few iterations of the loop and the program sticks in an infinite loop with mixed up pointers.

    In bubble sort, we iterate through the items several times. In each iteration, the largest item is bubbled up to the end of the list. After first iteration, we are sure that the largest element is at the end of the list. After second iteration, we are sure that the second largest element is before the last element of the list, and so on.
    You repeat this process until all the items are on their places:

    int getListSize(Node* start)
    {
        int count = 0;
        while(start != NULL)
        {
            count++;
            start = start->next;
        }
        return count;
    }
    
    void bubbleSort(Node *&start) // <-- Pass a reference to pointer, because we may need to modify the start pointer.
    {
        int size = getListSize(start);
        int i = 0;
    
        while(size--)
        {
            Node
                *current = start,
                *prev = NULL; // We are at the beginnig, so there is no previous node.
    
            while(current->next != NULL) // We have at least one node (size > 0) so `current` itself is not NULL.
            {
                Node *after = current->next;
                if((*current) > (*after))
                {
                    //swap the items
                    current->next = after->next;
                    after->next = current;
                    if (prev == NULL) // we are at the beginning
                        start = after;
                    else
                        prev->next = after;
    
                    prev = after;
                }
                else
                {
                    prev = current;
                    current = current->next;
                }
            }
        }
    }
    

    We repeat the “bubbling up” process size times. This is not the most efficient way, since we even compare the items that are already sorted. A more efficient way is to sort until no new swapping occurs:

    void bubbleSort(Node *&start) // <-- Pass a reference to pointer, because we may need to modify the start pointer.
    {
        int size = getListSize(start);
        int i = 0;
    
        Node *lastSwapped = NULL;
    
        while(size--)
        {
            Node
                *current = start,
                *prev = NULL, // We are at the beginnig, so there is no previous node.
                *currentSwapped = NULL;
    
            while(current->next != lastSwapped) // We have at least one node (size > 0) so `current` itself is not NULL.
            {
                Node *after = current->next;
                if((*current) > (*after))
                {
                    //swap the items
                    current->next = after->next;
                    after->next = current;
                    if (prev == NULL) // we are at the beginning
                        start = after;
                    else
                        prev->next = after;
    
                    prev = after;
                    currentSwapped = current;
                }
                else
                {
                    prev = current;
                    current = current->next;
                }
            }
    
            if (currentSwapped == NULL)
                break; // No swapping occured. The items are sorted.
            else
                lastSwapped = currentSwapped;
        }
    }
    

    This is the complete working program

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to sort a singly linked list which has been created and
I am trying to sort a Vector list using the Java Collections frameworks. This
I've been trying to get bubble sort a double link list, and i read
I am trying to sort a list of names followed by another string such
Trying to do this sort of thing... WHERE username LIKE '%$str%' ...but using bound
I am trying to correct the sort order of my ASP.NET drop down list.
So I'm trying to understand this simple merge and sort algorithm in python. Here's
So i have a singly linked list. New items are added to the front
I'm trying to make the letters of a matrix sort alphabetically and then be
Trying to sort this array of objects according to (1) depth and (2) weight,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.