4

I know what insertion sort is but I don't understand the code. And I searched it is all explanation about the sort but not the code. It will be easier for me if you answer the question step by step with examples! Thanks! Use this code for example. I put my questions in comment to make it clearer.

void insertionSort(int arr[], int n)
{
   int i, key, j;
   for (i = 1; i < n; i++)
   {
       key = arr[i];
       j = i-1;

       while (j >= 0 && arr[j] > key)
       {
           arr[j+1] = arr[j];
 /* if arr[j] > key, at here arr[j+1] has the number in arr[j] which is i
  * or key right? But now only arr[j+1] changed. There are only two same
  * numbers which is the bigger number, arr[j] is not changed to the smaller
  * number which arr[j+1] was.
  */
           j = j-1;
       }
       arr[j+1] = key;    
 /* so at here arr[j+1] now has the value of key which is arr[i], so the 
  * sorted arr[j+1] ended up unchanged? I know this step is supposed to 
  * change key to use it to compare numbers after.
  */
   }
}
chqrlie
  • 98,886
  • 10
  • 89
  • 149
jessie
  • 87
  • 10

2 Answers2

3

what you are calling key is a temporary variable storing the value of ith element I m uploading a picture that might help you understand it better

consider an array A[]={7,4,5,2} enter image description here

See you have initialized the variable i with 1 so key = arr[i]=arr1=4 in other words key is now 4.And also j=i-1 which will be 0. So moving on to the next statement while(j>=0 &&arr[j]>key) this checks if j is positive firstly and then checks if the value stored at jth position in the array(in this case is arr[0]=7) is greater than the key value then the loop initiates and now inside the loop body arr[j+1]=arr[j] in this statement the element of arr[j+1] which is arr1 is assigned the value of arr[0] which is 7. After this moving to the next statement j is then decreased by 1 so j is now -1. Due to which the while loop will break since the first condition is now false. Now moving out of the loop we see arr [j+1] which will be arr[0] is now assigned the "key" value which is 4. Similarly every time if there is a condition where the value of previous element(whose position is determined by i) is greater than the value of current element(whose position is determined by j) in array it is swapped. If ever the condition for the loop(arr[j] > key) is not fulfilled the statement arr[j+1]=key will mean nothing cause j+1 will then be equal to i. And the value will not be changed.

0

Your questions are a bit unclear, so let me try to explain what happens in the code, before attempting to answer them

  • The outer loop variable i, represents the start of the unsorted data in the array. It will be incremented from 1 to n through the for loop
  • key represents the first element from the unsorted region, which we are attempting to put in the correct spot after the while loop finishes
  • j represents the last index in the sorted region. It will be decremented to 0 through the while loop

The basic idea is to insert the next element in the unsorted part, by right shifting required elements from the sorted part till the final position of this new element

Now to your questions

if arr[j] > key, at here arr[j+1] has the number in arr[j]

YES

which is i or key right?

NO. arr[j+1] == key, only during the first iteration of the while, not after that

But now only arr[j+1] changed.

Yes and No. In one iteration of the loop, only arr[j+1] changed, but in the next iteration when j = j -1, the previous element changes or rather is shifted right

There are only two same numbers which is the bigger number, arr[j] is not changed to the smaller number which arr[j+1] was.

In a given iteration arr[j] does not change, but that does not mean that arr[j] does not change in the next iteration. For example, let's say j = 10 is the current iteration. so a[10] does not change in this iteration of the while loop, but it might change in the next iteration when j = 9 and a[10] = a[9]

so at here arr[j+1] now has the value of key which is arr[i]

No, arr[i] is potentially overwritten by the first iteration of the while loop, and hence was backed up in key

so the sorted arr[j+1] ended up unchanged? I know this step is supposed to change key to use it to compare numbers after.

This is invalid based on the above answer

Vikhram
  • 3,974
  • 1
  • 15
  • 30