为什么这个heapsort不起作用? [英] Why this heapsort is not working?

查看:72
本文介绍了为什么这个heapsort不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#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);
	
}





我的尝试:



当我尝试时,存储在LC和RC中的值不正确...!



What I have tried:

When I tried it,the Value stored in LC and RC are not correct...!

推荐答案

您的代码没有表现出你期望的方式,或者你不明白为什么!



有一个几乎通用的解决方案:一步一步地在调试器上运行你的代码,检查变量。 br />
调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。

调试器中没有魔法,它不是'知道你的代码应该做什么,它没有发现错误,它只是通过向你展示正在发生的事情来帮助你。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行,并在执行时检查变量。

调试器 - 维基百科,免费的百科全书 [ ^ ]



掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]



1.11 - 调试你的程序(步进和断点)|学习C ++ [ ^ ]



调试器仅显示您的代码正在执行的操作,您的任务是与应该执行的操作进行比较。
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.


[无耻的自我推销]

为了比较,您可以查看我的提示堆数据结构和堆排序 [ ^ ]。

[/无耻的自我推销]
[shameless self promotion]
For comparison, you may have a look at my tip Heap Data Structure and Heap Sort[^].
[/shameless self promotion]


这篇关于为什么这个heapsort不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆