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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:53:01+00:00 2026-06-12T15:53:01+00:00

I am just learning sorting (not for the first time). In bubble sort, we

  • 0

I am just learning sorting (not for the first time). In bubble sort, we have the following as the code.

int bubble_sort(int *arr, size_t n) {

    size_t i, j;
    for (i = 0; i < n; i++) {
        for (j = 0; j < n - 1; j++) {
            if (arr[j] > arr[j + 1]) {
                int temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }

    return 0;
}

As you could all see, the inner loop has n-1 times (in for loop), which is understandable (a[i],a[i-1] is both involved in one iteration), but the outer loop have i < n, but it also works for i < n-1. But most implementation in internet have n as the outer loop value. Doing the outer loop n-1 works fine for worst case 5 4 3 2 1. Just wondering, if there is any set of inputs that will not work for n-1 times of outer loop . If there are please post it and explain. Thanks.

  • 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-12T15:53:02+00:00Added an answer on June 12, 2026 at 3:53 pm

    N-1 is fine as well. As you describe, the worst case requires N-1 swaps (as the last has to become the first and vise-versa). If you add a print of the i-variable to the inside of the if-statement, you’ll see that it’s never called in the last iteration. This means that the last iteration of the loop never results in any swapping.

    An even more efficient implementation of Bubblesort does not use a for-loop as an outer loop, though. Have a look at the bit of code below. Can you see the execution difference?

    int bubble_sort(int *arr, size_t n) {
        size_t i,j;
        int flag = 1;
        while (flag) {
            flag = 0;
            for(j=0;j<n-1;j++) {
                if(arr[j] > arr[j+1]) {
                    int temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                    flag = 1;
                }
            }
        }
        return 0;
    }
    

    By setting the flag only if actual swapping occured, you’ll end up jumping out of the loop much sooner when you’re in an average case.

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

Sidebar

Related Questions

I am just learning to use Services, and am sort of stuck. I have
I'm just learning java and following a book. I have a program written via
Just learning php and looking into someone else's code. I'm not sure what is
Just learning C++, encountered an issue that i'm not really sure why the following
Just learning to add actions with code in my .m file and my first
Just learning C#, radiobuttons and checkboxes. No urgency. The code works to display the
I'm just learning MVC3 now and this is really confusing me. I have a
I'm just learning nHibernate and have come across what probably is a simple issue
just learning master page, i have a master page which include a content. but
Just learning WPF databinding and have a gap in my understanding. I've seen a

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.