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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:52:32+00:00 2026-06-11T13:52:32+00:00

I deduced the time complexity of bubble sort in its best case according to

  • 0

I deduced the time complexity of bubble sort in its best case according to the mothod used in book ALGORITHMS 2.2. But the answer turned out to be O(n^2).

Here’s my derivation, hope anyone can help me find out where is wrong:

public void bubbleSort(int arr[]) {
for(int i = 0, len = arr.length; i < len - 1; i++) {
    for(int j = 0; j < len - i - 1; j++) {
        if(arr[j + 1] < arr[j])
            swap(arr, j, j + 1);
    }
}

}

Statements                      cost    times
i = 0,len = arr.length          c1          1
i < len - 1                     c2          n
i++                             c3          n - 1
j = 0                           c4          n - 1
j < len - i - 1                 c5          t1(i=0) + t1(i=1) + ... + t1(i = n-2)
j++                             c6          t2(i=0) + t2(i=1) + ... + t2(i = n-2)
arr[j + 1] < arr[j]             c7          t3(i=0) + t3(i=1) + ... + t3(i = n-2)
swap(arr, j, j + 1)             c8          t4(i=0) + t4(i=1) + ... + t4(i = n-2)

T(n) = c1 + c2n + c3(n – 1) + c4(n – 1) + c5t5 + c6t6 + c7t7 + c8t8
= c1 + c2n + c3(n – 1) + c4(n – 1) + c5[t1(i=0) + t1(i=1) + … + t1(i = n-2)] + c6[t2(i=0) + t2(i=1) + … + t2(i = n-2)] + c7[t3(i=0) + t3(i=1) + … + t3(i = n-2)] + c8[t4(i=0) + t4(i=1) + … + t4(i = n-2)];

in its best cast, the sequence is already positive before sorting. Then t8 sould be 0.

T(n) = c1 + c2n + c3(n – 1) + c4(n – 1) + c5[t1(i=0) + t1(i=1) + … + t1(i = n-2)] + c6[t2(i=0) + t2(i=1) + … + t2(i = n-2)] + c7[t3(i=0) + t3(i=1) + … + t3(i = n-2)]

The time complexity is O(n^2)

  • 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-11T13:52:33+00:00Added an answer on June 11, 2026 at 1:52 pm

    Your implementation

    public void bubbleSort(int arr[]) {
        for(int i = 0, len = arr.length; i < len - 1; i++) {
            for(int j = 0; j < len - i - 1; j++) {
                if(arr[j + 1] < arr[j])
                    swap(arr, j, j + 1);
            }
        }
    }
    

    lacks the control whether there was any swap in the inner loop, and the breaking out of the outer loop if there wasn’t.

    That control makes it possible that the best case (an already sorted array) is O(n), since then there are no swaps in the inner loop when it runs the first time.

    public void bubbleSort(int arr[]) {
        boolean swapped = true;
        for(int i = 0, len = arr.length; swapped && i < len - 1; i++) {
            swapped = false;
            for(int j = 0; j < len - i - 1; j++) {
                if(arr[j + 1] < arr[j]) {
                    swap(arr, j, j + 1);
                    swapped = true;
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that sizeof is a compile-time calculation, but this seems odd to me:
I'm fighting a memory leak in a Python project and spent much time on
suppose i have one column in mysql database which is stated last time one
After a quick scan of related questions on SO, I have deduced that there's
I am having a bit of an issue with nested CASE in MySQL. The
I have been using DOM for a long time and as such DOM parsing
Possible Duplicate: Sort list using stl sort function My question is can we sort
I'm kind of new to Haskell and I'm having a hard time understanding what
Lets say you have a relational database of arbitrary but finite capacity, and the
Sometimes we have several classes that have some methods with the same signature, but

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.