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 6747099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:24:05+00:00 2026-05-26T12:24:05+00:00

I am having issues the last number in this code is not being sorted.

  • 0

I am having issues the last number in this code is not being sorted.

   // This is the more advanced optimzed version of bubble sort
        int modifiedBubbleSortArray(int array[])
        {

        int swapped = 0;

         do {
            swapped = false;
            // We specify a loop here for the sorting
            for(int i=0;i<NUM_ARRAYS;i++)
            {
                // We specify aother loop here for the sorting
                for(int j=0;j< i - 1 /* <<< here we make less comparisns on each pass each time */ ;j++)
                {
                    // If the array i is better than j we enter this swap
                    if (array[j]<array[j+1])  
                    {
                        // We swap here for the functions
                        swap(array[j], array[j+1]);
                        // We measure the amount of times we swap
                        amountOfSwaps += 1;
                        // If we swapped we break out of the loop
                        swapped = true;
                    }
                }
            }
             } while (swapped);
        printArray(array);
        return amountOfSwaps;

        }

        // Here we swap values to sort them
        void swap(int & value1, int & value2)
        {
            // We specify a temp and use it to swap
            int temp=value1; 
            value1=value2;
            value2=temp;

        }
  • 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-26T12:24:05+00:00Added an answer on May 26, 2026 at 12:24 pm

    The inner loop should be:

    if (array[j]>array[j+1]) 
    { 
      // We swap here for the functions 
      swap(array[j], array[j+1]); 
      // We measure the amount of times we swap 
      amountOfSwaps += 1; 
    } 
    

    Give that a shot and see if your variant on bubble sort sorts correctly.

    To sort in descending order, just change the if condition:

    if (array[j]<array[j+1]) 
    

    One more bug is the inner for loop. Change that to:

    for(int j=0;j<=i-1;++j)
    

    Update (based on last change with swap test added):

    bool swapped = false; 
    
    // We specify a loop here for the sorting 
    for(int i=0;i<NUM_ARRAYS;i++) 
    { 
        // We specify aother loop here for the sorting 
        for(int j=0;j<=i-1;++j)
        { 
            // If the array i is better than j we enter this swap 
            if (array[j]<array[j+1])   
            { 
                // We swap here for the functions 
                swap(array[j], array[j+1]); 
                // We measure the amount of times we swap 
                amountOfSwaps += 1; 
                // If we swapped we break out of the loop 
                swapped = true; 
            } 
        } 
        // If no swaps this iteration, break out
        if (!swapped)
            break;
    } 
    

    If you really want a good sort, look into introspection sort – a variant on quick sort. The algorithm is more complicated, but the sort is also more efficient, being O(N log N) instead of O(N^2) which is the complexity of bubble sort.

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

Sidebar

Related Questions

Im having issues getting this to work, maybe its not even possible? I have
I've been having issues with this for far too long. This code should output
For the last couple months i have ben having some issues with my app.config
Having issues referencing $(this) from within a the nested ajax 'success' function... I know
Still having issues with this problem. Please help if you can. So I am
I've been having a bug in my code for some time and could not
I'm having issues with this regex it doesn't seem to grab the titles (everything
I'm asking this as a very last resort as I have not been able
Just in the last week I have been having issues with accessing a JBoss
I am having issues trying to get a database connection using the code below:

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.