0% found this document useful (0 votes)
8 views1 page

Heap

The document contains a C program that implements the heap sort algorithm to sort a string input by the user. It defines functions for maintaining the heap property and sorting the string. The sorted string is then printed as output.

Uploaded by

Karthikeyan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views1 page

Heap

The document contains a C program that implements the heap sort algorithm to sort a string input by the user. It defines functions for maintaining the heap property and sorting the string. The sorted string is then printed as output.

Uploaded by

Karthikeyan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

#include<stdio.

h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
#define lc(i)(2*(i)+1)
void percdown(char*,int,int);
void heap(char*,int);
void main()
{
char a[100];
int i,j,n,tmp;
clrscr();
printf("enter the string\n");
scanf("%s",a);
n=strlen(a);
heap(a,n);
printf("SORTED STRING:\n");
printf("%s",a);
getch();
}
void heap(char a[],int n)
{
int i,tmp;
for(i=n/2;i>=0;i--)
percdown(a,i,n);
for(i=n-1;i>0;i--)
{
tmp=a[0];
a[0]=a[i];
a[i]=tmp;
percdown(a,0,i);
}
}
void percdown(char a[],int i,int n)
{
int c;
int t;
for(t=a[i];lc(i)<n;i=c)
{
c=lc(i);
if(c!=n-1&&a[c+1]>a[c])
c++;
if(t<a[c])
a[i]=a[c];
else
break;
}
a[i]=t;
}

You might also like