answersLogoWhite

0

Write a c program on heap sort?

User Avatar

Anonymous

12y ago
Updated: 8/17/2019

#include<stdio.h>

#include<conio.h>

void heapsort(int a[],int n);

void heapcreate(int a[],int n);

void adjust(int a[],int n);

int main()

{

int a[20],n,temp,i;

printf("enter number of elements :");

scanf("%d",&n);

printf("enter the elements\n");

for(i=0;i <n;i++)

scanf("%d",&a[i]);

heapsort(a,n);

printf("sorted array is \n");

for(i=0;i<n;i++)

{

printf("%d\n",a[i]);

}

getch();

return(0);

}

void heapsort(int a[],int n)

{

int i,temp;

heapcreate(a,n);

for(i=n-1;i>0;i--)

{

temp=a[0];

a[0]=a[i];

a[i]=temp;

adjust(a,i);

}

//return;

}

void heapcreate(int a[],int n)

{

int i,j,k,item;

for(k=1;k

{

item=a[k];

i=k;

j=(i-1)/2;

while(i>0&&item>a[j])

{

a[i]=a[j];

i=j;

j=(i-1)/2;

}

a[i]=item;

}

//return;

}

void adjust(int a[],int n)

{

int i,j,item;

j=0;

item=a[j];

i=2*j+1;

while(i<=n-1)

{

if(i+1<=n-1)

if(a[i]

i++ ;

if(item

{

a[j]=a[i];

j=i;

i=2*j+1;

}

else

break;

}

a[j]=item;

//return;

}

User Avatar

Wiki User

12y ago

What else can I help you with?

Related Questions

Write a program of binary heap in c or c language?

to implement operations on binary heap in c


What is the C program for heap sort using recursion?

123


Write a program in c to sort the details of 10 students using the structure?

for(i = 0; i &lt; num_students; i ++){ sort(student[i]); } That's what you get when you're that specific!


Different types of sorting techniques in c language?

types of sorting in c language are: insertion sort selection sort bubble sort merge sort two way merge sort heap sort quick sort


Write c program to find median?

If you are using an array : sort using qsort() then take middle element.


A C program using dynamic memory allocation to sort n names in ascending order?

Writing a C program that uses dynamic memory allocation to sort names in ascending order is a typical computer science assignment. To write this program, you must be in UNIX.


Write a c program for creating a virus?

a c program for creat a virus


How do you write a c plus plus program to sort a vector of strings using MSD radix sort?

The standard library sort algorithm automatically uses MSD radix to sort strings: std::vector&lt;std::string&gt; vs = {"a", "b", "c" "d", "ab"}; std::sort(vs.begin(), vs.end()); After sorting, the order will be: {"a", "ab", "b", "c", "d"}


Where do we write main function in a c program?

Into the source program.


Write a c program to sort an unsorted stack?

A stack is implicitly sorted by hierarchical nested order. It does not make sense to sort a stack. Do you mean a list? If so, please ask the question again.


Write a c program to find Volume and surface area of cube?

Write a c program to compute the surface area and volume of a cube


How do you write a c program to analysis a circuit?

There is no need to write a C program for circuit analysis; there are lots of packages out there that already do that (for example, Spice).