Problem:
Insertion Sort

Write a program of the Insertion Sort algorithm which sorts a sequence A in ascending order. The algorithm should be based on the following pseudocode:

for i = 1 to A.length-1
key = A[i]
/* insert A[i] into the sorted sequence A[0,...,j-1] */
j = i - 1
while j >= 0 and A[j] > key
A[j+1] = A[j]
j--
A[j+1] = key

Note that, indices for array elements are based on 0-origin.

To illustrate the algorithms, your program should trace intermediate result for each step.

Input

The first line of the input includes an integer N, the number of elements in the sequence.

In the second line, N elements of the sequence are given separated by a single space.

Output

The output consists of N lines. Please output the intermediate sequence in a line for each step. Elements of the sequence should be separated by single space.

Constraints

1 ≤ N ≤ 100

Sample Input 1

6
5 2 4 6 1 3

Sample Output 1

5 2 4 6 1 3
2 5 4 6 1 3
2 4 5 6 1 3
2 4 5 6 1 3
1 2 4 5 6 3
1 2 3 4 5 6

Sample Input 2

3
1 2 3

Sample Output 2

1 2 3
1 2 3
1 2 3

Hint

Template in C

Solution in JAVA (judge verdict: WA: Presentation Error)






import java.io.*;
public class Main {

    public static void main(String[] args) throws IOException  {

        // ?¨??????\??????????????????????????????
    	//??£?¨?
        int c;
        int keta;
        int j;
        int deru;
        int koumoku_suu = 1000;//?????????????????????????????°?????\???
        int hairetsu_keta[] = new int[koumoku_suu];//??°???????????°?????\???
        int hairetsu[] = new int[koumoku_suu];//1???????????°????????\???
        int hairetsu_fugou[] = new int[koumoku_suu];//?¬???????????????¨??????????????¨????????? ???????¬???????????????????????????\???
        int hairetsu_suuchi[] = new int[koumoku_suu];//?¬???????????????¨??????????????¨????????? ???????¬???????????????????????????\???
        int k;
        int m;
        int n;
        
      //?????????
        keta = 0;
        j = 0;
        deru = 0;
        k = 0;
        m = 0;
        n = 0;
        
        for (int ii = 0; ii < koumoku_suu; ii++)
        {
        	hairetsu_keta[ii] = 0;
        	hairetsu[ii] = 0;
        	hairetsu_fugou[ii] = 1;
        }
        //???????????????
        int i= 0;
        while ((c = System.in.read()) != -1  )
        {
            // System.out.println(c);
            if ( c > 47 && c < 58) // suuji ga kita
               {
                hairetsu[i] = c-48;
                i++;
                keta++;
               }
            if ( c == 45) // minus ga kita
               {
                hairetsu_fugou[j] = -1;
               }

            if ( c == 10) // return ga kita
            {
                hairetsu_keta[j] = keta;
                deru++; //return ga  kitara while wo deru
                j++;
            }
            if ( c == 32) //space ga kita
            {

                hairetsu_keta[j] = keta;
                keta = 0;//space ga kitara tsugi no moji no tame keta ha sokomade
                j++;
            }
            if (deru > 0)
            {
                break;
                }
        }
           // ?¨??????\?????????????????????????????????????????????

        // ??°??????????????\??????
        //int hairetsu_suuchi[] = new int[4]; hairetsu_suuchi[0] = 0; hairetsu_suuchi[1] = 0; hairetsu_suuchi[2] = 0; hairetsu_suuchi[3] = 0;
        
        
     


        while( n < (j + 1))
        {
        while(j > 0 && hairetsu_keta[n] > 0 )
        {
            hairetsu_suuchi[k] = hairetsu_suuchi[k]*10 + hairetsu[m];
            hairetsu_keta[n] = hairetsu_keta[n] - 1;
            m++;
        }
        if (hairetsu_keta[n] == 0)
        {
            if(hairetsu_fugou[n] == -1)
            {
                hairetsu_suuchi[k] = hairetsu_suuchi[k]*(-1);

            }
            n++;
            k++;
        }
        }

        //??°??????????????\???????????????
        /*
          for (int p = 0; p < k - 1 ; p++)

        {
        System.out.println("???????????°??????" + hairetsu_suuchi[p]);
        }

        */
        int n_gyou = 0;
        n_gyou = hairetsu_suuchi[0];
        // System.out.println("n_gyou???" + n_gyou);
        
        // 2???????????????
      //?????????
        keta = 0;
        j = 0;
        deru = 0;
        for (int ii = 0; ii < koumoku_suu; ii++)
        {
        	hairetsu_keta[ii] = 0;
        	hairetsu[ii] = 0;
        	hairetsu_fugou[ii] = 1;
        	hairetsu_suuchi[ii] = 0;
        }
        //???????????????
         i= 0;
        while ((c = System.in.read()) != -1  )
        {
            // System.out.println(c);
            if ( c > 47 && c < 58) // suuji ga kita
               {
                hairetsu[i] = c-48;
                i++;
                keta++;
               }
            if ( c == 45) // minus ga kita
               {
                hairetsu_fugou[j] = -1;
               }

            if ( c == 10) // return ga kita
            {
                hairetsu_keta[j] = keta;
                deru++; //return ga  kitara while wo deru
                j++;
            }
            if ( c == 32) //space ga kita
            {

                hairetsu_keta[j] = keta;
                keta = 0;//space ga kitara tsugi no moji no tame keta ha sokomade
                j++;
            }
            if (deru > 0)
            {
                break;
                }
        }
           // ?¨??????\?????????????????????????????????????????????

        // ??°??????????????\??????
        //int hairetsu_suuchi[] = new int[4]; hairetsu_suuchi[0] = 0; hairetsu_suuchi[1] = 0; hairetsu_suuchi[2] = 0; hairetsu_suuchi[3] = 0;
        
        
        
        k = 0;
        m = 0;
        n = 0;


        while( n < (j + 1))
        {
        while(j > 0 && hairetsu_keta[n] > 0 )
        {
            hairetsu_suuchi[k] = hairetsu_suuchi[k]*10 + hairetsu[m];
            hairetsu_keta[n] = hairetsu_keta[n] - 1;
            m++;
        }
        if (hairetsu_keta[n] == 0)
        {
            if(hairetsu_fugou[n] == -1)
            {
                hairetsu_suuchi[k] = hairetsu_suuchi[k]*(-1);

            }
            n++;
            k++;
        }
        }

        //??°??????????????\???????????????
       int p;
          for ( p = 0; p < k - 1 ; p++)

        {
        System.out.print(hairetsu_suuchi[p] + " ");
        }
          System.out.println("");
        // insert sort
         
          int i2;
          int v;
          int j2;
          
          for (i2 = 1;i2 < n_gyou;i2++)
          {
        		  v =  hairetsu_suuchi[i2];
        		  j2 = i2 - 1;
        		  while( j2 >= 0 && hairetsu_suuchi[j2] > v)
        		  {
        		  hairetsu_suuchi[j2+1]	 = hairetsu_suuchi[j2];
        		  j2--;
        		  hairetsu_suuchi[j2 + 1] = v;
        		  }
        		  System.out.print(hairetsu_suuchi[0]);
        	  	for (p = 1; p < k - 1 ; p++)
        	  		{
        		  		System.out.print(" " + hairetsu_suuchi[p]);
        	  		}
        	  System.out.println();
          	}
    }
        
        
    }