Advertisement
Guest User

Untitled

a guest
May 31st, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.58 KB | None | 0 0
  1. #include <iostream>
  2. #include <conio.h>
  3. using namespace std;
  4.  
  5. typedef struct nodeO* oper;
  6. struct nodeO {
  7.     int timp;
  8.     int index;
  9.     oper next;
  10. };
  11.  
  12. typedef struct nodeM* masini;
  13. struct nodeM {
  14.     int TimpTot;
  15.     int Lista[100];
  16.     masini next;
  17. };
  18.  
  19. void min_heapify(int *a,int i,int n)
  20. {
  21.     int j, temp;
  22.     temp = a[i];
  23.     j = 2 * i;
  24.     while (j <= n)
  25.     {
  26.         if (j < n && a[j+1] < a[j])
  27.             j = j + 1;
  28.         if (temp < a[j])
  29.             break;
  30.         else if (temp >= a[j])
  31.         {
  32.             a[j/2] = a[j];
  33.             j = 2 * j;
  34.         }
  35.     }
  36.     a[j/2] = temp;
  37.     return;
  38. }
  39. void build_minheap(int *a, int n)
  40. {
  41.     int i;
  42.     for(i = n/2; i >= 1; i--)
  43.     {
  44.         min_heapify(a,i,n);
  45.     }
  46. }
  47.  
  48. void max_heapify(int *a, int i, int n)
  49. {
  50.     int j, temp;
  51.     temp = a[i];
  52.     j = 2 * i;
  53.     while (j <= n)
  54.     {
  55.         if (j < n && a[j+1] > a[j])
  56.             j = j + 1;
  57.         if (temp > a[j])
  58.             break;
  59.         else if (temp <= a[j])
  60.         {
  61.             a[j / 2] = a[j];
  62.             j = 2 * j;
  63.         }
  64.     }
  65.     a[j/2] = temp;
  66.     return;
  67. }
  68. void build_maxheap(int *a,int n)
  69. {
  70.     int i;
  71.     for(i = n/2; i >= 1; i--)
  72.     {
  73.         max_heapify(a,i,n);
  74.     }
  75. }
  76.  
  77. int main()
  78. {
  79.     int n, i, x;
  80.     cout<<"enter no of elements of array\n";
  81.     cin>>n;
  82.     int a[20];
  83.     for (i = 1; i <= n; i++)
  84.     {
  85.         cout<<"enter element"<<(i)<<endl;
  86.         cin>>a[i];
  87.     }
  88.     build_minheap(a, n);
  89.     cout<<"Min Heap\n";
  90.     for (i = 1; i <= n; i++)
  91.     {
  92.         cout<<a[i]<<endl;
  93.     }
  94.     _getch();
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement