Click here to Skip to main content
14,102,245 members
Rate this:
 
Please Sign up or sign in to vote.
See more:
#include<stdio.h>

struct node{
	int item;
};
int N;

void Max_Heapify( struct node StrArr[] , int i ) {
	//printf("i=%d\n",i);
	int LC=2*i;
	int RC=(2*i)+1;
	int Largest;
	//printf("Values i %d,LC %d, Rc %d,");
	
	if( LC<=N && StrArr[LC].item > StrArr[i].item ) {
		
		Largest=LC;
		
	}
	
	if( RC<=N && StrArr[RC].item > StrArr[LC].item ) {
		
		Largest = RC;
		
	}
	//printf("Largest %d\n\n");
	
	if( Largest != i ){
		
		int temp = 	StrArr[Largest].item ;
		StrArr[Largest].item = StrArr[i].item ;
		StrArr[i].item = temp ;
		
		Max_Heapify(StrArr, Largest) ;
	}
	
}

void Build_Max_Heap( struct node StrArr[] ) {
	
	int i;
	for( i=N/2 ; i >= 1 ; i-- ){
		//printf("Call %d:\n",i);
		Max_Heapify( StrArr , i);
	}
	
}

void HeapSort( struct node StrArr[] ) {
	 	
	 	Build_Max_Heap(StrArr);
	
		while(N>=1) {
			
			int temp = 	StrArr[1].item ;
			StrArr[1].item = StrArr[N].item ;
			StrArr[N].item = temp ;
		
			printf("%d ",StrArr[N].item);
			
			Max_Heapify( StrArr , 1);
			N=N-1;
	
		}
}

main() {
	
	printf("Enter size of the Array:");
	scanf("%d",&N);
	struct node StrArr[N];
	int i;
	printf("\nEnter Elements of the Array- ");
	for( i = 1 ; i <= N ; i++ ) {
		scanf("%d",&StrArr[i].item);
	}
	/*for( i = 1 ; i <= N ; i++ ) {
		printf("%d",StrArr[i].item);
	}*/
	printf("\nArray After HeapSort:");
	HeapSort(StrArr);
	
}


What I have tried:

When I tried it,the Value stored in LC and RC are not correct...!
Posted
Updated 16-Nov-18 2:19am
Comments
Patrice T 16-Nov-18 0:18am
   
define "the Value stored in LC and RC are not correct...!"
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.
Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
   
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

[shameless self promotion]
For comparison, you may have a look at my tip Heap Data Structure and Heap Sort[^].
[/shameless self promotion]
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web05 | 2.8.190518.1 | Last Updated 16 Nov 2018
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100