/*  Small C+ Insertion sort!
 *
 *  Joe Mackay 3/2/1999
 */

#include <stdio.h>

#define SIZE    100

main()
{
        int A[SIZE], i, j, tmp;

        /* Fill array with descending numbers (worst case) */
        i=0;
        while (i<SIZE)
                A[i] = SIZE-i++;

        /* Print initial state of array */
        puts("Before:\n\l");
        printarray(A);

        /* Now, er, sort it */
        for (i=1; i<SIZE; i++)
        {
                tmp = A[i];
                j=i;
                while (j!=0 && A[j-1]>tmp)
                        A[j]=A[--j];
                A[j] = tmp;
        }

        /* Print sorted version */
        puts("After:\n\l");
        printarray(A);

}

printarray(int A[])
{
        int i;

        i=0;
        while (i<SIZE)
        {
                printf("%d ", A[i++]);
        }
        puts("\n\l");
}
