MENU
Question -

Write a function to sort any array of n elements using insertion sort. Array should be passed as argument to the function.



Answer -

void insertsort (int a[],int n)
 {
 int p,ptr;
 //Assuming a[0]=int_min i.e. smallest integer
 for(p = 1; p<=n;p++)
 {
 temp=a[p];
 ptr=p-1;
 while (temp
 {
 a [ptr+1]=a[ptr] ; //Move Element
 Forward
 ptr--;
 }
 a[ptr+1]=temp; //Insert Element in Proper Place
 }

Comment(S)

Show all Coment

Leave a Comment

Free - Previous Years Question Papers
×