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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:19:14+00:00 2026-06-12T11:19:14+00:00

Followings are written in a ppt about Insertion Sort in my class: void insertionSort(DataType

  • 0

Followings are written in a ppt about Insertion Sort in my class:

void insertionSort(DataType theArray[], int n) {
  for (int unsorted = 1; unsorted < n; ++unsorted) {

    DataType nextItem = theArray[unsorted];
    int loc = unsorted;

    for (;(loc > 0) && (theArray[loc-1] > nextItem); --loc)
       theArray[loc] = theArray[loc-1];

    theArray[loc] = nextItem;
  }
}

–

Running time depends on not only the size of the array but also the contents of the array.
Best-case:       O(n)
Array is already sorted in ascending order.
Inner loop will not be executed.
>>>> The number of moves: 2*(n-1)        O(n)
>>>> The number of key comparisons: (n-1)    O(n)
Worst-case:      O(n2)
Array is in reverse order:
Inner loop is executed p-1 times, for p = 2,3, …, n
The number of moves: 2*(n-1)+(1+2+...+n-1)= 2*(n-1)+ n*(n-1)/2   O(n2)
The number of key comparisons: (1+2+...+n-1)= n*(n-1)/2          O(n2)
Average-case:    O(n2)
We have to look at all possible initial data organizations.
So, Insertion Sort is O(n2)

What exacly are move and key comparison?? I couldn’t find an explanaiton on Google.

  • 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-12T11:19:16+00:00Added an answer on June 12, 2026 at 11:19 am

    Let me word the algorithm first.

    • Assume at a given time there are two part of array. index 0 to index loc - 1 is sorted in ascending order and index loc to n - 1 is unsorted.
    • Start with element at loc, find its correct place in sorted part of the array and insert it there.

    So now there are two loops:

    1. First outer loop, starts with loc = 1 to loc = n, basically partitions the array in sorted and unsorted part.
    2. Second inner loop finds position of element at loc in the sorted part of array ( 0 to loc - 1).

    For the inner loop, to find correct location, you have to compare element at loc with, in worst case, all the elements in sorted part of array. This is key comparison.

    To insert, you have to create a void in sorted part of the array for element at loc. This is done by swapping each element in sorted part to the next element. This is move.

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

Sidebar

Related Questions

I've written following class to start my application activity Home.class but on device start
I have the following class written by someone else that I'm trying to understand
I am using the following class written by Mark Brittingham for two way AES
I have written following code for getting location name : UseGpsActivity.java public class UseGpsActivity
I've written a function foreach that accepts a lambda function ala: void foreach(void (*p)(pNode))
I written following code to send sms. private void sendSMS(String phoneNumber) { String SENT
Consider the following class written in c# .net 4.0 (typically found in a nhibernate
I have written following xaml code: <Window x:Class=WpfApplication3.MainWindow xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml Title=MainWindow Height=200 Width=200> <StackPanel>
I've written following code: @Controller @RequestMapping(/page{number}) public class IndexController { @RequestMapping(method = RequestMethod.GET) public
I have written following app.js class to get 3 tabs in my app using

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.