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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:09:10+00:00 2026-06-15T03:09:10+00:00

I found two bubble algorithms but I not sure which one is right… First

  • 0

I found two bubble algorithms but I not sure which one is right…

First example:

for (int i = 1; i < input.Length-1; i++)
    for (int j = 0; j < input.Length - 1 - i; j++)
        if (input[j] > input[j + 1])
            swap(input, j, j + 1);

Second example:

for (int i = 1; i < input.Length-1; i++)
    for (int j = 0; j < input.Length - 1; j++)
        if (input[j] > input[j + 1])
            swap(input, j, j + 1);

Wikipedia Example

For input: 5 1 4 2 8

First example: 6 comparisons

Second example: 12 comparisons

First Pass:

( 5 1 4 2 8 ) –> ( 1 5 4 2 8 ), Swap since 5 > 1

( 1 5 4 2 8 ) –> ( 1 4 5 2 8 ), Swap since 5 > 4

( 1 4 5 2 8 ) –> ( 1 4 2 5 8 ), Swap since 5 > 2

( 1 4 2 5 8 ) –> ( 1 4 2 5 8 )

Second Pass:

( 1 4 2 5 8 ) –> ( 1 4 2 5 8 )

( 1 4 2 5 8 ) –> ( 1 2 4 5 8 ), Swap since 4 > 2 <– Example 1 stop here

( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )

( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )

Third Pass

( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )

( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )

( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )

( 1 2 4 5 8 ) –> ( 1 2 4 5 8 ) <– Example 2 stop here

Edit: when I say which one is right I mean which one is the original bubble sort

  • 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-15T03:09:11+00:00Added an answer on June 15, 2026 at 3:09 am

    First algorithm is incorrect.
    It will fail if last element in the array is not the greatest.

    Specifically, iteration on ‘j’:

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

    seems to skip last element.
    If your input is {0, 2, 2, 1}
    Then your input.Length = 4

    for loop above will have condition j < 4 – 1 – i, where i = 1 so j < 4 – 1 – 1, so j < 2
    so last j used will be j = 1, last comparison will be input[1] > input[2] -> input[3] is never compared.

    you can fix it by changing for loop to j <= input.Length – 1 – i

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

Sidebar

Related Questions

I have found two ways on keeping the screen on: First one is simpler:
After some research about algorithms I found two terms which confuses me. I've read
I have found two ways. Which one is better? Is there any other way
I have found two different things in two well known books in c, first
I've found two plugins in CakePHP using recaptcha, but they are outdated. How can
a) I found two definitions of schema: FIRST - A set of information that
I found two ways of passing command-line arguments into a character array: int main
i have a problem with spring autowire feature which found two beans with the
While integrating a Django app I have not used before, I found two different
I have created two functions that sorts a list using bubble sort, but I

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.