/*AN IMPLEMENTATION TO SHOW SORTING IN ASCENDING FORM & ALSO INSERTION USING POINTER ...*/
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i,j,x,flag;
int *ptr,*sort;
ptr=malloc(5*sizeof(int));//MALLOC THE TWO POINTER SO THAT VALUE COULD BE PUT INTO THESE POINTERS
sort=malloc(5*sizeof(int));//5 ELEMENTS MEANS MALLOC OF 5 * sizeof(int)
for(i=0;i<5;i++)
{
printf("enter the value");//enter elements which are in random form into 1st pointer
scanf("%d",(ptr+i));//(ptr+i) is address of every element in pointer ...using i..address of every element is changing ,...bcz value of i is changing...we cant use ptr++...bcz if we ++ment base address of pointer itself then how will we get...same pointer ptr value next time ...when we have to compare...--ment of ptr again& again is not a good way..
//so use i which is varying value for this address of pointer
}
*(sort+0)=*(ptr+0);//put base value 1st of all into new pointer
for(i=0;i<5;i++)//loop inside loop means each element will be compaired atmost 5 no. of times to get exact position of old pointer element into new pointer element..//
{
for(j=0;j<=i;j++)
{
if(*(ptr+i)< *(sort+j))//5<0//if value of old pointer element is less ..& new pointer elements are greater then shift all other values of new pointer sort on right hand side ..& put this small value of old pointer into new pointer every time on left hand side
//from starting to end ...new array will remain sorted ....we will search the exact position of old pointer into new pointer by compairing ///if i find by compairing old pointer value is small ..1st time ...all other big elements in new array are shifted to right side to put ..this small value on left side....
{//flag=1;
//break;
}
}//if(flag==0)
i=x;x--;//make right shift ....its elements as always done..i contains no. of elements in old array statred from ..0 position...so loop takes place 1 less time so x--..is done..//
for(;j<=x;x--)
{
*(ptr+x+1)=*(ptr+x);//5 th element to 6 postion //4 to 5// 3 to 4//upto the position where element is to be inserted is shifted to right position
}
*(sort+j)=*(ptr+i);//at last position last element is inserted
//(sort+j+1)=(sort+j);
break;//break when element is inserted & enter into loop of i