title: this heapsort program in c not working? i use pointer to edit array elements
body: <pre><code>#include&lt;stdio.h&gt; 
  #include&lt;string.h&gt;  
  #define PARENT(i) (i-1)/2  
  #define LEFT(i) 2*i  
  #define RIGHT(i) 2*i+1  
  void swap(int *a,int *b)       //swapping function//
  {  
  int t;  
  t=*a;  
  *a=*b;    
  *b=t;  
  }  
  void   HEAP_SORT(int a[])         //sorting the heap//
  {   
  int i,len=0;
   while(a[++len]!='\0')
           //b is the sorted array//
  BUILD_MAXHEAP(&amp;a);  
  for(i=len;i&gt;1;i--)  
  {  
  MAX_HEAPIFY(&amp;a,i,len);  
  swap(&amp;a[1],&amp;a[i]);  
  len--;  
  b[len-i] =a[1];  
  MAX_HEAPIFY(&amp;a,1);  
  }  
  for(i=0;b[i]!='\0';i++)  
      printf("%d,",b[i]);  
  }  
  void MAX_HEAPIFY(int *a,int i,int len)  
  {  
  int L,largest,r;  
  L=LEFT(i);  
  r=RIGHT(i);  
  if(L&lt;=len &amp;&amp; *(a+L)&gt;=*(a+i))  
  {  
  largest=L;}  
  else  
  largest =i;  
  if(r&lt;=len &amp;&amp; *(a+r)&gt;*(a+largest))  
  largest=r;  
  if(largest!=i)  
  {  
  swap(&amp;a[i],a+largest);}  
  MAX_HEAPIFY(a,largest,len);  
  }  
  void BUILD_MAXHEAP(int *a)  
  {  
  int len=0,j;  
  for(len=0;*(a+len)!='\0';len++)  
      ;
  int i;  
  len--;  
  for(i=len/2;i&gt;=1;i--)  
  MAX_HEAPIFY(a,i,len);  
  }  
  main()  
  {  
      int a[]={1,2,2,2,4,2,87,3,2,3,-1,3,-4};  
      HEAP_SORT(a);  
      return 0;  
  }
  </code></pre>
  
  <p>This is the program for heap sorting the array a[].  I used a pointer so that it (original array) heapsort can be edited, but it is not giving the desired output. What are the logical errors in this program?</p>