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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:33:23+00:00 2026-06-10T08:33:23+00:00

I read Introduction to Algorithms by Cormen&Co and implement algorithm on java. I wonder

  • 0

I read “Introduction to Algorithms” by Cormen&Co and implement algorithm on java. I wonder does it make sense to write into insertion sort code if-statement at the final set() method? I want make code more faster if it is possible.

public static void insertion(List<Integer> a) {
    List<Integer> aList = a;
    int temp;
    int previousIndex;

    for (int i = 1; i < aList.size(); i++) {
        temp = aList.get(i);
        previousIndex = i - 1;
        while ((previousIndex >= 0) && aList.get(previousIndex) > temp) {
            aList.set(previousIndex + 1, a.get(previousIndex));
            previousIndex--;
        }

        //if(aList.get(previousIndex + 1) > temp){
            aList.set(previousIndex + 1, temp);
        //}

    }
}

Excuse me, if it is elementary. I am very beginner.

  • 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-10T08:33:24+00:00Added an answer on June 10, 2026 at 8:33 am

    This looks like an attempt to optimize the code, making it faster. The apparent idea is to make a test on the data to avoid setting it. This would make execution faster only if (1) reading and testing the value is faster than setting the value and (2) there is enough cases when the test will avoid setting the value.

    IMO (1) is dubious and (2) would need to be very often to be save much time, and I do not think that is the case.

    A better faster test could be achieved by testing (previousIndex + 1 != i) instead of (aList.get(previousIndex + 1) > temp), and that may be a good optimization if the copying of objects of the list would be much more time consuming, like copying complex object structures. However, in out case, we are talking about Integer objects, so my take is that adding the test will only slow down things in all cases.

    Of course, as it is the case with all optimizations, only real life measurements will tell what is effective, and that is why optimization is always the last thing to do, and only when necessary.

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

Sidebar

Related Questions

I just read about the breadth-first search algorithm in the Introduction to Algorithms book
I am reading Introduction to Algorithms and read about the insertion sort. I tried
I need to read A Practical Introduction to Data Structures and Algorithm Analysis by
Recently I have read about hash-tables in a very famous book Introduction to Algorithms
I have to check for character sequences like \chapter{Introduction} from the strings read from
Introduction first, question at the end. Please read carefully! I have a master-detail relation
Every time you read the introduction to an MVC web framework, you meet the
How to set key (legend) font size in gnuplot? I read the gnuplot introduction
I read many articles to understand Java servlet but I did not succeed. Can
Recently I read that the sign $ is allowed in Java variable names, 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.