힙정렬 질문입니다.
튼트나
렌덤 함수 를 줘서 삽입정렬과 힙정렬을 정렬을하고 정렬횟수도 구하는것이에요힙정렬 부분에서 무슨문제인지는 모르겟는데 2번째 배열이 마지막으로 가집니다.질문 내용 : 2번째배열이 마지막으로 가지고 정렬이 좀 안되는 거 같은데 어디인지 확인좀 부탁드려요
#includestdio.h
#includestdlib.h
#includetime.h
#define a_max 11
void insertion();
void heap(int);
void heapsort(int,int);
void swap(int*,int*);
int harr[a_max],iarr[a_max];
int hcnt=0,icnt=0;
void main(void)
{
file *out;
int i, data[a_max];
srand(time(null));
out = fopen(c:/aa/1111.text,w);
for(i=0; ia_max; i++) data[i]=rand()%a_max+1;
printf(\n);
for(i=0;ia_max;i++)
{
iarr[i]=harr[i]=data[i];
printf(%d ,data[i]);
}
for(i=(a_max-1)/2;i0;i--);{
heapsort(i,a_max-1);
}
heap(a_max-1);
fprintf(out,힙정렬 : );
for(i=0;ia_max;i++)
fprintf(out,%d ,harr[i]);
fprintf(out,\n힙정렬 수 : %d,hcnt);
insertion();
fprintf(out,\n삽입정렬 : );
for(i=0;ia_max;i++)
fprintf(out,%d, ,iarr[i]);
fprintf(out,\n삽입정렬 수 : %d,icnt);
}
void insertion()
{
int i, j, temp;
for(i=1;ia_max;i++)
{
temp=iarr[i];
j=i;
while((j0)&&(iarr[j-1]temp))
{
iarr[j]=iarr[j-1];
j--;
icnt+=1;
}
iarr[j]=temp;
}
}
void heap(int end)
{
int temp[a_max]={0};
int i,j=1;
int bufend=end;
for(i=end;i=1;i--)
{
temp[j++]=harr[1];
swap(&harr[1], &harr[i]);
heapsort(1, i-1);
}
for(i=1;ibufend;i++)
{
harr[i]=temp[i];
}
}
void heapsort(int parent, int end)
{{
int smaller;
int left = parent*2;
int right = parent*2+1;
if(rightend)
{
if(harr[left] harr[right])
{
smaller = right;
}
else
{
smaller = left;
}
}
else if(leftend)
{
smaller = left;
}
else{
return;
}
if(harr[smaller] harr[parent])
{
swap( &harr[smaller], &harr[parent]);
hcnt++;
}
heapsort(smaller,end);
}
void swap(int *a, int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
}