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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:26:35+00:00 2026-05-26T08:26:35+00:00

I was trying a basic sorting exercise and I was hoping I could receive

  • 0

I was trying a basic sorting exercise and I was hoping I could receive some help with what is probably a basic logic error.

int[] numbers = new int[] { 2, 5, 11, 38, 24, 6, 9, 0, 83, 7 };
        for (int loop = 0; loop < numbers.Length; loop++)
        {
            Console.WriteLine(numbers[loop]);
        }
        Console.WriteLine("Performing a bubble sort");

        bool flag = false;
        do
        {

            for (int loop = 0; loop < numbers.Length - 1; loop++)
            {
                if (numbers[loop] > numbers[loop + 1])
                {

                    int temporary = numbers[loop];
                    numbers[loop] = numbers[loop + 1];
                    numbers[loop + 1] = temporary;
                    flag = true;
                }

            }
        } while (flag == false);

        for (int loop = 0; loop < numbers.Length; loop++)
        {
            Console.WriteLine(numbers[loop]);
        }
  • 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-26T08:26:35+00:00Added an answer on May 26, 2026 at 8:26 am

    There are two issues with your code. The first, as has been pointed out, is that you need to loop as long as flag == true. That would have been a lot more clear if you had given it a more expressive name. madeASwap or something like that makes it obvious: do while(madeASwap).

    The other issue is that you need to reset the flag before running the inner loop. Without that, just checking for false ends after a single iteration, and checking for true results in an infinite loop.

    In short: reset your flag, and loop while it’s true.

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

Sidebar

Related Questions

Rather new to REST and Jersey, and I'm trying out some basic examples. I've
I'm very new to ruby and trying some basic stuff. When I send HTTP
Im trying to get into some basic JavaFX game development and I'm getting confused
I am trying to learn some of the basic and advanced features of visual
Trying to do some really basic math here, but my lack of understanding of
Trying to get some basic understanding of console functionalities. I am having issues so
Trying to follow some basic examples and getting stuck by this code that is
I'm trying to do some basic paging in MSSQL. The problem I'm having is
I'm trying to implement basic auditing with some of my models (like CreatedAt, UpdatedAt,
I am new to Objective C. I am trying to draw(basic lines as of

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.