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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:54:03+00:00 2026-05-20T05:54:03+00:00

i know insertion sort isn’t that great…but even still…what are two more simple improvements

  • 0

i know insertion sort isn’t that great…but even still…what are two more simple improvements that could be made to the sort below?

public static void insertion_sort_with_moves(int[] arr){
    for (int i = 1; i <= arr.length; i++){
        int v = arr[i-1];
        int j = i-1;
        for (/*declared j outside loop*/; j > 0; j--) {
            //compswap(a[j-1], a[j]);
            if (v < arr[j-1]) arr[j] = arr[j-1];
            else break;
        }
        arr[j] = v;
    }
}
  • 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-05-20T05:54:04+00:00Added an answer on May 20, 2026 at 5:54 am

    A few micro-optimizations are:

    1,2,3)

        int len = arr.length; 
    ...
    
        for (int i = 0; i < len; ++i){
                int v = arr[i];
                int j = i;
    

    Saves you from computing i-1 two times and ++i is faster than i++.
    Not sure about the length thing (could save the offset addition when accessing a class member).

    4,5)

    for (/*declared j outside loop*/; j != 0; --j) {
    

    j!=0 should be faster than j>0 (really don’t expect much) and –j is faster than j–.

    Well most of them may be platform dependent and may make no difference at all.

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

Sidebar

Related Questions

I know that it's a subject that can raise a lot of debate, but
I want to know why we always use Sorting algorithm like (Insertion Sort or
I know there are some like: Bubble sort Insertion sort Shell sort Merge sort
I know bubble sort is probably not the fastest way to do this but
I know using prepared statements helps to avoid sql-injection. My problem is, that a
I have done inserting a record into database but I don't know how to
Know this might be rather basic, but I been trying to figure out how
I know this is fairly subjective, but I'm diving into testing and learning about
Just got my feet wet in sorting algorithm with Haskell. I've implemented insertion-sort and
I want to write an insertion sort and a merge sort in clisp. The

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.