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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:35:05+00:00 2026-06-17T12:35:05+00:00

I am having a hard time understanding why best case of insertion sort in

  • 0

I am having a hard time understanding why best case of insertion sort in o(n) ?

     for (int i = 0; i < size; i++) {

                for (int j = i; j > 0; j--) {
                    int k = j-1;
                        if( a[j] < a[k]){
                            int temp = a[j];
                            a[j] = a[k];
                            a[k] = temp;
                        }

                }
     }

Lets consider an example initial array [1,2,3,4,5] size = 5
first loop will go from i = 0 to size – 1
and second loop will go from i to 1 but lets assume, inner for loop also goes from 0 to size – 1 in other words inner for loop also executes (n-1) times similar to outer for loop
I agree there will be no swaps but there will be Comparison’s, & it will be exactly equal as unsorted array ?
then n-1 (outer loop) * n – 1(inner loop) = n^2 – n + 1 = O(n^2)
can any one explain me where i m wrong ?

  • 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-17T12:35:06+00:00Added an answer on June 17, 2026 at 12:35 pm

    Your code always runs in O(n^2). You have to break the inner for loop at the time you have found the place where the element should be.

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

Sidebar

Related Questions

I'm having a hard time understanding this. Consider the following example: protected void Page_Load(object
I'm having a hard time understanding why #include <iostream> using namespace std; int fib(int
I am having a hard time understanding how font-size is working in the following
I'm having a hard time understanding the size parameter of mmap (sorry my C
Im having a hard time understanding the limits and possibilities of an IF statement.
I am having a hard time understanding the nuances of git-fetch. I understand that
I am having hard time in understanding Wait() , Pulse() , PulseAll() . Will
I'm having a hard time understanding why the following code doesn't work. I'm sure
I'm having a really hard time understanding delegates and object inheritance (if I may
I am having hard time getting hasErrors to work with indexed properties. For example

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.