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

The Archive Base Latest Questions

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

it’s a insertion sort algorithm I understand forwarding the greatest no to next index

  • 0

it’s a insertion sort algorithm

I understand forwarding the greatest no to next index but cant understand when it moves forward how its previous position (index) is being taken by the smaller number that was just compare e.g in list [2,1] 2 moves to next index by list[j+1]=list[j]; but how 1 moves backward or to previous index

 //unsorted array   
int[] list = new int[] { 5, 2, 4, 6, 1 };   

// the key element being sorted   
int key;   
// 
//start looping starting from the second element  
for (int i = 1; i < list.Length; i++)   
{  
    key = list[i];//store the key 
    int j = i - 1;//get the previous index  
    //
    //loop until you meet a smaller number or 0 
    while (j >= 0 && list[j] > key)  
    { 
        //move the greater number forward  
        list[j + 1] = list[j];  
        // Decrementing  
        j--;   
    }  
    //set the key in the proper index  
    list[j + 1] = key; 
}
  • 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:18:48+00:00Added an answer on June 17, 2026 at 12:18 pm

    That is done in the last line inside the loop. After moving one or more (or even zero) items forward, the current value is put in the correct position.

    For example, if you have sorted the array up to the last item, and it looks like this:

    2, 4, 5, 6, 1
    

    The value 1 is copied into the key variable, then items are copied forward one by one:

    2, 4, 5, 6, -   <-  the empty slot here still contains 1, but it's unused
    2, 4, 5, -, 6   <-  the empty slot here still contains 6, but it's unused
    2, 4, -, 5, 6
    2, -, 4, 5, 6
    -, 2, 4, 5, 6
    

    Now the value from the key variable is placed where the last item was copied from:

    1, 2, 4, 5, 6
    

    The theory behind the insert sort is to take items from one array and insert into a new array. The implementation that you are using is only using a single array, and you can think of it as divided into a sorted part and an unsorted part. The sorted part starts out with the size zero:

    [][ 5, 2, 4, 6, 1 ]
    

    when the array is sorted, items are picked from the unsorted part and are inserted at the right place in the sorted part. The sorted part grows and the unsorted part shrinks, until the unsorted part is empty:

    [ 5 ][ 2, 4, 6, 1 ]
    [ 2, 5 ][ 4, 6, 1 ]
    [ 2, 4, 5 ][ 6, 1 ]
    [ 2, 4, 5, 6 ][ 1 ]
    [ 1, 2, 4, 5, 6 ][]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I'm trying to select an H1 element which is the second-child in its group
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I want to construct a data frame in an Rcpp function, but when I

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.