title: Implementing min Heap using an array
body: <p>I am trying to implement min Heap in C. </p>
  
  <pre><code>// Heaps:
  // node i -&gt; 2i and 2i+1 children
  // 2i//2 = i and 2i+1//2 = i same parents
  
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  #define max 100
  
  int heap[max];
  int size = 0;
  
  void heapifyUp(){ // last element
      int i = size;
  
      while(i){ // 
          int parent = i/2;
          if (heap[parent]&lt;heap[i]){
              int t = heap[parent];
              heap[parent] = heap[i];
              heap[i]=t;
          }        
      }
  }
  
  void heapifyDown(){ // top element
      int i = 0;
  
      while(i&lt;=size){
          int c1 = 2*i;
          int c2 = 2*i + 1;
          int t = 0;
          if (heap[c1]&gt;=heap[c2]){
              t = c2;
          }
          else{
              t = c1;
          }
          int temp = heap[i];
          heap[i] = heap[t];
          heap[t] = temp;
          i = t;
      }
  }
  
  void insert(int key){
      size = size + 1;
      heap[size] = key;
      heapifyUp();
  }
  
  int returnMin(){
      return heap[0];
  }
  
  int deleteMin(){
      int t = heap[0];
      heap[0] = heap[size];
      size = size - 1;
      heapifyDown();
      return t;
  }
  
  void printHeap(){
  
      int i = 0;
      while(i&lt;=size){
          printf("%d",heap[i]);
          i = i + 1;
      }
  }
  
  int main(){
      insert(10);
      insert(20);
      insert(11);
      insert(7);
      insert(18);
  
      printHeap();
      printf("%d",deleteMin());
  
      insert(110);
      insert(-7);
      insert(15);
  
      printf("%d",deleteMin());
  }
  </code></pre>
  
  <p>The issue is that when I run the program, I get no output and the program doesn't terminate.</p>
  
  <p>I think i have implemented the logic correctly.</p>
  
  <p>Using debugger with C is hard as I am on a Mac (doesn't support Codeblocks, never really understood how to use gdb, am just using the built-in gcc compiler on a text editor), so I am stuck in this issue.</p>
  
  <p>Thanks for your help. </p>