các bác ơi e cài đặt heap_sort ko bit sai tai dau ma cho ra ket quả toàn sai
PHP Code:
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<time.h>
#define Max 10
/****************************/
void init(int a[],int n){
for(int i=0;i<n;i++)
a[i]=rand()%100;
}
/***********************/
void print(int a[],int n){
for(int i=0;i<n;i++)
printf("%d ,",a[i]);
}
/*********************************************/
void swap(int *a,int *b ){
int temp;
temp=*a;
*a=*b;
*b=temp;
}
/*******************************************/
void max_heapify(int a[],int i,int n){
int left,right,largest=i;
left=2*1+1;
right=2*i+2;
if(left<n&&a[left]>a[largest]) largest=left;
if(right<n&&a[right]>a[largest]) largest=right;
if(largest!=i) swap(&a[largest],&a[i]);
}
/*******************************************/
void heap_sort(int a[],int n){
for(int i=n/2;i>=0;i--)
max_heapify(a,i,n);
for(i=n-2;i>=0;i--){
swap(&a[0],&a[i+1]);
max_heapify(a,0,i+1);
}
}
/******************************************/
main(){
int n,a[Max];
clrscr();
printf("\n vao so phan tu cua mang ");
scanf("%d",&n);
init(a,n);
printf("\ncac phan tu luc ban dau la\n");
print(a,n);
printf("\nmang sau khi sap xep la:\n");
heap_sort(a,n);
print(a,n);
getch();
return 0;
}
các bác giúp e với e sắp thi rùi.thanks nhìu