heap sort 질문이요!!도와주세요!!
핫보라
질문 제목 : 질문 내용 :
typedef struct{
int key;
}element;element list[26]= {0, 4, 21, 14, 3, 23, 20, 10, 17, 19,
22, 9, 38, 26, 5, 16, 8, 6, 24, 25, 15, 11, 7, 12, 13, 18};int a1 = 26;
void adjust(element list[], int root, int a1)
{
int child, rootkey, i;
element temp1;
temp1 = list[root];
rootkey = list[root].key;
child = 2 * root; /* left child */
while(child = a1){
if (child a1 && list[child].key list[child+1].key)
child++;
if (rootkey list[child].key)
break;
else {
list[child/2] = list[child];
child *= 2;
} /* else */
} /* while */
list[child/2] = temp1;
}
void heapsort(element list[], int a1)
{
/* perform a heapsort on the array */
int i, j;
element temp; for (i = a1/2; i 0; i--)
adjust(list, i, a1);/* initial heap construction */
printf(initial heap\n);
for(i=1;i26;i++)
printf(%d , list[i]);
printf(\n\n);
for (i = a1 - 1; i 0; i--) /* heap adjust */
{
swap(list[1], list[1+i], temp);
adjust(list, 1, i);
}
}int main(void)
{
int i; heapsort(list, a1); printf(after sorting heap\n);
for(i=1;i26;i++)
printf(%d , *(list+i)); return 0;
}작성한 코드는 이렇구요 이걸 실행하면이렇게 나오거든여.. initial heap 은 잘나온것 같은데 after sorting heap에서 자꾸 가운데 10이 두번 출력이되요 한번만 출력이 되어야하는데 ..
이 10이 어디서 나온건지도 모르겠고.. 왜출력이 되는지모르겠어요;
도와주세요ㅠ