Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <conio.h>
- int a[20];
- int n;
- int dummy;
- void display()
- {
- int i;
- for(i = 0;i < n;i++)
- printf("%d ",a[i]);
- }
- void build_heap(int k)
- {
- int i;
- for(i = k / 2;i >= 0;i--)
- {
- max_heapify(i,k);
- }
- }
- max_heapify(int i,int size)
- {
- int rc,lc,max,idx,temp;
- max = a[i];
- idx = i;
- rc = i * 2 + 2;
- lc = i * 2 + 1;
- if(lc >= size)
- return;
- else
- {
- if(a[i] < a[lc])
- {
- max = a[lc];
- idx = lc;
- }
- if(rc < size)
- {
- if(max < a[rc])
- {
- max = a[lc];
- idx = rc;
- }
- }
- if(idx != i)
- {
- temp = a[i];
- a[i] = a[idx];
- a[idx] = temp;
- max_heapify(idx,size);
- }
- }
- }
- heap_sort()
- {
- int temp,i;
- while(n != 0)
- {
- temp = a[0];
- a[0] = a[n - 1];
- a[n - 1] = temp;
- n--;
- build_heap(n);
- }
- for(i = 0;i < dummy;i++)
- printf("%d ",a[i]);
- }
- int main()
- {
- int i,inc,dec,incr,decr,found=1;
- printf("\n enter number of elements ");
- scanf("%d",&n);
- dummy = n;
- printf("\n enter the numbers: ");
- for(i = 0;i < n; i++)
- scanf("%d",&a[i]);
- display();
- printf("\n heap is\n");
- build_heap(n);
- display();
- printf("\nsorted list is: ");
- heap_sort();
- printf("\n enter the value you want to increase : ");
- scanf("%d",&inc);
- for(i=0;i<dummy;i++)
- {
- if(a[i] == inc)
- {
- found = 1;
- break;
- }
- }
- if(found != 1)
- printf("\nelement does not exist in the heap!\n");
- else
- {
- printf("\n enter the value : ");
- scanf("%d",&incr);
- if(a[i] > incr)
- printf("\nentered number is smaller than actual!");
- else
- {
- for(i=0;i<dummy;i++)
- {
- if(a[i] == inc)
- break;
- }
- a[i] = incr;
- build_heap(dummy);
- found = 0;
- for(i = 0;i < dummy;i++)
- printf("%d ",a[i]);
- }
- }
- // imcrease ends
- printf("\n enter the value you want to decrease : ");
- scanf("%d",&dec);
- for(i=0;i<dummy;i++)
- {
- if(a[i] == dec)
- {
- found = 1;
- break;
- }
- }
- if(found != 1)
- printf("\nelement does not exist in the heap!\n");
- else
- {
- printf("\n enter the value : ");
- scanf("%d",&decr);
- if(a[i] < decr)
- printf("\nentered number is greater than actual!");
- else
- {
- for(i=0;i<dummy;i++)
- {
- if(a[i] == dec)
- break;
- }
- a[i] = decr;
- build_heap(dummy);
- found = 0;
- for(i = 0;i < dummy;i++)
- printf("%d ",a[i]);
- }
- }
- getch();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement