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

The Archive Base Latest Questions

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

I just finished the old Sorting Detective homework (where you are given a few

  • 0

I just finished the old “Sorting Detective” homework (where you are given a few black box sorting algorithms and have to determine what example each one is by the results), and I noticed that Insertion Sort always took N-1 comparisons on a sorted list. Since I won’t be able to look at my instructor’s code until everyone has turned in their assignments, nor am I allowed to ask questions in class that could tip off the other students as to how to proceed to solve the problem, this left me with a question I can’t get the answer to this question for at least a week.

Is it always the case in the real world that a textbook example of Insertion Sort will do N-1 comparisons on a sorted list, or is it quirk of my instructor’s/textbook’s version of Insertion Sort?

After searching Google and the Wikipedia, I couldn’t find the answer to this, which means either I’m asking the wrong question or they just don’t have it. Any ideas?

  • 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-12T03:01:13+00:00Added an answer on June 12, 2026 at 3:01 am

    Yes, a “textbook” insertion sort will perform N-1 comparisons if the input is already sorted. See for example Knuth’s The Art of Computer Programming, Volume 3, §5.2.1, Algorithm S (Straight insertion sort). For each record Ri (i > 1), the algorithm compares it against elements Ri-1 down to R1, in that order, until it finds an element smaller than Ri. If the input is already sorted, then Ri-1 is always smaller than Ri, so the algorithm only performs one comparison per record (and no comparisons for record 1).

    Of course, you could write insertion sort to perform the comparisons in order from R1 to Ri-1 instead, but that would perform N*(N-1)/2 comparisons if the input list were already sorted. So don’t do that. 🙂

    You could also use a binary search to find the location at which to insert each record. This is called binary insertion, and Knuth also discusses it in §5.2.1. Again, it will perform more comparisons on an already-sorted input than textbook straight insertion (but fewer on random input).

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

Sidebar

Related Questions

I just finished reading the HTML5 Developer's Cookbook and have a question. I've read
Just finished up a recent homework, but I know it could be more efficient.
Just finished reading Crockford's JavaScript: The Good Parts and I have a question concerning
I have just finished programming an import/export feature for an existing complex website. The
Just finished reading this blog post: http://www.skorks.com/2010/03/an-interview-question-that-prints-out-its-own-source-code-in-ruby/ In it, the author argues the case
Just finished up my first mvc4 app. Everything is working great until I deploy
Just finished reading Jon Skeet's article about events and delegates and got a question.
I just finished this script it works well, but I am struggling to pass
I just finished looking at this question: https://stackoverflow.com/questions/753122/which-cloud-computing-platform-should-i-choose But, I am not certain what
I just finished setting up my Eclipse with the Android plug-in. (i.e https://dl-ssl.google.com/android/eclipse/ )

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.