Insertion Sort

Insertion Sort:

#include<stdio.h>
int main()
{
    int ar1[20],ar2[20],i,j,k,cnt,n=0;
   
    while(scanf("%d",&ar1[n])==1)        //Infinite Loop. So, use condition
    {
        if(ar1[n]<0)
            break;
        n++;
    }
   
    /*insertion algorithm*/
   
    ar2[0]=ar1[0];
    for(i=1;i<n;i++)
    {
        cnt=0;
        for(j=0;j<i;j++)
        {
            if(ar1[i]<=ar2[j])
            {
                cnt++;
                for(k=i;k>j;k--)
                {
                    ar2[k]=ar2[k-1];
                }
                ar2[j]=ar1[i];
                break;
               
            }
        }
        if(cnt==0)
            ar2[i]=ar1[i];
       
    }
   

    //This program run n*n*n. Because three loops
    // In bubblel sort run n*n. So, this insertion is not Right
   
   
    for(j=0;j<n;j++)
    {
        printf("%d ",ar2[j]);
    }
    printf("\n");
   
    return 0;
}
// YOU SEE DETTEL TO KNOW MORE.

মন্তব্যসমূহ