DOWNLOAD PROGRAM
MORE DATA STRUCTURE PROGRAM IN C
CODING:
#include<stdio.h>
#include<conio.h>
#define max 11
void restore_heap_up(int *,int);
void restore_heap_down(int *,int,int);
void main()
{
int heap[max],n,i,j,temp;
clrscr();
printf("How Many Number You want to ADD.->");
scanf("%d",&n);
printf("Input Number.\n");
for(i=1;i<=n;i++)
{
printf("[%d]=",i);
scanf("%d",&heap[i]);
restore_heap_up(heap,i);
}
j=n;
for(i=1;i<=j;i++)
{
temp=heap[1];
heap[1]=heap[n];
heap[n]=temp;
n=n-1;
restore_heap_down(heap,1,n);
}
n=j;
printf("SORTED LIST IS...\n");
for(i=1;i<=n;i++)
{
printf("[%d]=%d\n",i,heap[i]);
}
getch();
}
void restore_heap_up(int *heap,int index)
{
int val=heap[index];
while((index>1) && (heap[index/2]<val))
{
heap[index]=heap[index/2];
index/=2;
}
heap[index]=val;
}
void restore_heap_down(int *heap,int index,int n)
{
int val=heap[index];
int j=index*2;
while(j<=n)
{
if((j<n) && (heap[j]<heap[j+1]))
j++;
if(heap[j]<heap[j/2])
break;
heap[j/2]=heap[j];
j=j*2;
}
heap[j/2]=val;
}
MORE DATA STRUCTURE PROGRAM IN C
CODING:
#include<stdio.h>
#include<conio.h>
#define max 11
void restore_heap_up(int *,int);
void restore_heap_down(int *,int,int);
void main()
{
int heap[max],n,i,j,temp;
clrscr();
printf("How Many Number You want to ADD.->");
scanf("%d",&n);
printf("Input Number.\n");
for(i=1;i<=n;i++)
{
printf("[%d]=",i);
scanf("%d",&heap[i]);
restore_heap_up(heap,i);
}
j=n;
for(i=1;i<=j;i++)
{
temp=heap[1];
heap[1]=heap[n];
heap[n]=temp;
n=n-1;
restore_heap_down(heap,1,n);
}
n=j;
printf("SORTED LIST IS...\n");
for(i=1;i<=n;i++)
{
printf("[%d]=%d\n",i,heap[i]);
}
getch();
}
void restore_heap_up(int *heap,int index)
{
int val=heap[index];
while((index>1) && (heap[index/2]<val))
{
heap[index]=heap[index/2];
index/=2;
}
heap[index]=val;
}
void restore_heap_down(int *heap,int index,int n)
{
int val=heap[index];
int j=index*2;
while(j<=n)
{
if((j<n) && (heap[j]<heap[j+1]))
j++;
if(heap[j]<heap[j/2])
break;
heap[j/2]=heap[j];
j=j*2;
}
heap[j/2]=val;
}
Comments
Post a Comment