Advertisement
Guest User

heap_sort_Sanjoy

a guest
Sep 2nd, 2015
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.16 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <conio.h>
  3.  
  4. int a[20];
  5. int n;
  6. int dummy;
  7. void display()
  8. {
  9.      int i;
  10.      for(i = 0;i < n;i++)    
  11.      printf("%d ",a[i]);
  12.      
  13. }
  14.  
  15. void build_heap(int k)
  16. {
  17.      int i;
  18.      for(i = k / 2;i >= 0;i--)
  19.      {
  20.           max_heapify(i,k);      
  21.      }        
  22. }
  23.  
  24. max_heapify(int i,int size)
  25. {
  26.      int rc,lc,max,idx,temp;
  27.      
  28.      max = a[i];
  29.      idx = i;
  30.      rc = i * 2 + 2;
  31.      lc = i * 2 + 1;
  32.      
  33.      if(lc >= size)
  34.            return;
  35.      else
  36.      {
  37.            if(a[i] < a[lc])
  38.            {
  39.                    max = a[lc];
  40.                    idx = lc;        
  41.            }
  42.            if(rc < size)
  43.            {
  44.                  if(max < a[rc])
  45.                  {
  46.                     max = a[lc];  
  47.                     idx = rc;      
  48.                  }  
  49.            }  
  50.                 if(idx != i)
  51.                     {
  52.                           temp = a[i];
  53.                           a[i] = a[idx];
  54.                           a[idx] = temp;  
  55.      
  56.                           max_heapify(idx,size);
  57.                     }
  58.      }          
  59. }
  60.  
  61. heap_sort()
  62. {
  63.      int temp,i;
  64.  
  65.      while(n != 0)
  66.      {
  67.                temp = a[0];
  68.                a[0] = a[n - 1];
  69.                a[n - 1] = temp;
  70.  
  71.                n--;
  72.               build_heap(n);            
  73.      }
  74.      for(i = 0;i < dummy;i++)
  75.      printf("%d ",a[i]);    
  76. }    
  77.  
  78. int main()
  79. {
  80.      int  i,inc,dec,incr,decr,found=1;
  81.          printf("\n enter number of elements ");
  82.          scanf("%d",&n);  
  83.          dummy = n;
  84.          printf("\n enter the numbers: ");
  85.          for(i = 0;i < n; i++)
  86.          scanf("%d",&a[i]);
  87.  
  88.          display();          
  89.          printf("\n heap is\n");
  90.         build_heap(n);
  91.          
  92.          display();
  93.          
  94.         printf("\nsorted list is: ");
  95.        heap_sort();
  96.          
  97.          printf("\n enter the value you want to increase : ");
  98.          scanf("%d",&inc);
  99.          
  100.          for(i=0;i<dummy;i++)
  101.          {
  102.                 if(a[i] == inc)
  103.                 {
  104.                         found = 1;
  105.                         break;
  106.                 }
  107.          }
  108.          
  109.          if(found != 1)
  110.          printf("\nelement does not exist in the heap!\n");
  111.          else
  112.          {
  113.              printf("\n enter the value : ");
  114.              scanf("%d",&incr);
  115.              
  116.              if(a[i] > incr)
  117.                      printf("\nentered number is smaller than actual!");
  118.              else
  119.              {        
  120.                       for(i=0;i<dummy;i++)
  121.                       {
  122.                             if(a[i] == inc)
  123.                             break;
  124.                       }
  125.          
  126.                       a[i] = incr;
  127.                       build_heap(dummy);
  128.          
  129.                       found = 0;
  130.                      
  131.                           for(i = 0;i < dummy;i++)
  132.                            printf("%d ",a[i]);
  133.              }
  134.          }
  135.          
  136.        // imcrease ends
  137.        
  138.         printf("\n enter the value you want to decrease : ");
  139.          scanf("%d",&dec);
  140.          
  141.          for(i=0;i<dummy;i++)
  142.          {
  143.                 if(a[i] == dec)
  144.                 {
  145.                         found = 1;
  146.                         break;
  147.                 }
  148.          }
  149.          
  150.          if(found != 1)
  151.          printf("\nelement does not exist in the heap!\n");
  152.          else
  153.          {
  154.              printf("\n enter the value : ");
  155.              scanf("%d",&decr);
  156.              
  157.              if(a[i] < decr)
  158.                      printf("\nentered number is greater than actual!");
  159.              else
  160.              {        
  161.                       for(i=0;i<dummy;i++)
  162.                       {
  163.                             if(a[i] == dec)
  164.                             break;
  165.                       }
  166.          
  167.                       a[i] = decr;
  168.                       build_heap(dummy);
  169.          
  170.                       found = 0;
  171.                      
  172.                           for(i = 0;i < dummy;i++)
  173.                            printf("%d ",a[i]);
  174.              }
  175.          }
  176.        
  177.          getch();
  178.          return 0;
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement