0% found this document useful (0 votes)
31 views2 pages

Ascending Heap Tree Sort in 'C'

The document describes creating an ascending heap tree and sorting data using heap sort. It includes code to build a max heap from an array, swap the first and last elements on each pass, and sift down elements to maintain the heap order property.

Uploaded by

shardul kukarni
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)
31 views2 pages

Ascending Heap Tree Sort in 'C'

The document describes creating an ascending heap tree and sorting data using heap sort. It includes code to build a max heap from an array, swap the first and last elements on each pass, and sift down elements to maintain the heap order property.

Uploaded by

shardul kukarni
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/ 2

/* Assignment 05 -- Create Ascending Heap Tree

And Sort The Data 'C'.


Name:- Shardul Kulkarni Div:- B, Roll
No:- 28.*/

#include<stdio.h>

int main(){
int Heap[30],Num,i,Last,Temp;

printf("\nHow Many Number's You Want To Enter In Your Array: ");


scanf("%d",&Num);

printf("\nEnter Elements In Array:\n");


for(i=1;i<=Num;i++)
scanf("%d",&Heap[i]);

//create a heap
Heap[0]=Num;
Create(Heap);

//sorting
while(Heap[0] > 1){
//swap heap[1] and heap[last]
Last=Heap[0];
Temp=Heap[1];
Heap[1]=Heap[Last];
Heap[Last]=Temp;
Heap[0]--;
Down_Adjust(Heap,1);
}
printf("\nArray After Heap Sort:\n");
for(i=1;i<=Num;i++)
printf("%d ",Heap[i]);
return 0;
}
void Create(int Heap[]){
int i,Num;
Num=Heap[0]; //no. of elements

for(i=Num/2;i>=1;i--)
Adjust(Heap,i);
}
void Down_Adjust(int Heap[],int i){
int j,Temp,Num,Flag=1;
Num=Heap[0];

while(2*i<=Num && Flag==1){


j=2*i; //j points to left child
if(j+1<=Num && Heap[j+1] > Heap[j])
j=j+1;
if(Heap[i] > Heap[j])
Flag=0;
else{
Temp=Heap[i];
Heap[i]=Heap[j];
Heap[j]=Temp;
i=j;
}
}
}

/*
OUTPUT:

How Many Numbers You Want To Enter In Your Array: 6

Enter Elements In Array:


5
3
1
8
7
2

Array After Heap Sort:


1 2 3 5 7 8

How Many Numbers You Want To Enter In Your Array: 7

Enter Elements In Array:


12
10
8
9
7
4
2

Array After Heap Sort:


2 4 7 8 9 10 12

How Many Numbers You Want To Enter In Your Array: 10

Enter Elements In Array:


16
21
40
3
2
5
3
18
17
16

Array After Heap Sort:


2 3 3 5 16 16 17 18 21 40
*/

You might also like