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

  • SEARCH
  • Home
  • 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 1064971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T19:44:32+00:00 2026-05-16T19:44:32+00:00

I know bubble sort is probably not the fastest way to do this but

  • 0

I know bubble sort is probably not the fastest way to do this but its acceptable. i’m just having trouble with adjusting the algorithm to double link lists from arrays.

My double linked lists have a type int and a type string to hold a number and a word. My list was sorted with an insertion sort that I wrote to sort alphabetically, now I need to reorder my double linked list numerically, greatest to least.

My trouble spot is how to run this loop so that it is properly sorted thoroughly and not just one time.

here’s what i’ve managed to get down so far:

void DblLinkedList::ReorderListNumeric()
{
dummy = new Node();
temphead = head;
temp = head->next;

while(tempTwo->next != NULL)
{
    if(temp->wordCount < tempTwo->wordCount)
    {               
        dummy->word = tempTwo->word;
        dummy->wordCount = tempTwo->wordCount;

        tempTwo->word = temp->word;
        tempTwo->wordCount = temp->wordCount;

        temp->word = dummy->word;
        temp->wordCount = dummy->wordCount;
    }
    temp = tempTwo;
    tempTwo = tempTwo->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-05-16T19:44:32+00:00Added an answer on May 16, 2026 at 7:44 pm

    My trouble spot is how to run this loop so that it is properly sorted thoroughly and not just one time.

    If you already have a loop that will successfully do one pass and swap is okay, the usual way to do multiple passes relatively efficiently is:

    set swapped = true
    while swapped:
        set swapped = false
        do your one pass, setting swapped to true whenever you swap
    

    This avoids the naive n2 solution that beginners will invariably start with.

    And that’s it really.

    You set swapped to true so that you initially enter the list then set it to false immediately, inside the loop.

    Your single pass will set swapped only if a swap takes place. If no swap takes place in your pass, then the list is sorted and you exit the loop.

    If any swap takes place, the swapped flag is set and you will need to run another pass. This is because the swap could be at the end of the list and invalidate the order earlier on, such as:

    Initial: 1   2   3   4   6   7   5
      Pass1: 1   2   3   4   6   5<=>7 (swap)
      Pass2: 1   2   3   4   5<=>6   7 (swap)
      Pass3: 1   2   3   4   5   6   7 (no swap, so exit loop)
    

    So, assuming your code is correct, start with something like:

    void DblLinkedList::ReorderListNumeric() {
        Node *ptr, *dummy = new Node();
    
        // Zero or one element, no sort required.
    
        if (head == NULL) return;
        if (head->next == NULL) return;
    
        // Force initial entry.
    
        int swapped = 1;
        while (swapped) {
            // Flag as last time, then do one pass.
    
            swapped = 0;
    
            ptr = head;
            while (ptr->next != NULL) {
                if (ptr->wordCount < ptr->next->wordCount) {
                    // Swapping, need another pass.
    
                    swapped = 1;
    
                    dummy->word = ptr->word;
                    ptr->word = ptr->next->word;
                    ptr->next->word = dummy->word;
    
                    dummy->wordCount = ptr->wordCount;
                    ptr->wordCount = ptr->next->wordCount;
                    ptr->next->wordCount = dummy->wordCount;
                }
                ptr = ptr->next;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Howdy, I do know how to implement a simple bubble-sort for 1dimensional array. But
I don't know if this is possible or not, but here is the code
Know this might be rather basic, but I been trying to figure out how
(I know this sounds like a duplicate, but hear me out) Given the following
I know there are some like: Bubble sort Insertion sort Shell sort Merge sort
i write a c++ code for Bubble sort algorithm and i dont know how
I would like to know how to implement Bubble sort onto a DefaultTableModel .
So this is pretty basic, but I don't really know C. I want to
Does anyone know how to implement the standard bubble message that warns users whenever
Know of any good libraries for this? I did some searches and didn't come

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.