힙정렬 질문입니다.
튼트나
렌덤 함수 를 줘서 삽입정렬과 힙정렬을 정렬을하고 정렬횟수도 구하는것이에요힙정렬 부분에서 무슨문제인지는 모르겟는데 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;
}
번호 | 제 목 | 글쓴이 | 날짜 |
---|---|---|---|
2655184 | 이 세개 프로그램좀 해결해주실.... 고수님들???? (3) | 말달리자 | 2024-05-17 |
2655129 | 다차원 배열 원소들 주소계산하는거 질문이요~ | 터전 | 2024-05-17 |
2655103 | AVRstudio MoveAxis. MoveLine 함수질문 | 앨버트 | 2024-05-16 |
2655072 | 콘솔창의 위치와 크기를 정하고 싶습니다. | 딥레드 | 2024-05-16 |
2655044 | 자료구조랑 online judge site에 대해서 질문있습니다... (1) | 뽀송뽀송 | 2024-05-16 |
2655016 | 별찍기 문제 중 이해안되는 부분이 있어요... | 도전중 | 2024-05-16 |
2654986 | 입력받은 성적을 배열에 저장하기 (3) | 안녕나야 | 2024-05-15 |
2654959 | ===========-이런거 미리 지정하는법 (1) | 김자영 | 2024-05-15 |
2654930 | 도와주십시요 ㅜㅜ 자료구조 ㅜㅜ | 도널드 | 2024-05-15 |
2654881 | PID 모듈 검색 부분 질문합니다 . | 이름없음 | 2024-05-14 |
2654850 | 조건에 만족하는 다음 수를 찾고 싶습니다. | 초엘 | 2024-05-14 |
2654790 | 두파일간 byte단위 txt파일 복사 中 | 슬찬 | 2024-05-14 |
2654768 | 사전만들기 입니다. | 여우By | 2024-05-13 |
2654716 | c언어 고수님들 답좀적어주세요 급합니다 ㅠㅠ | 한말글 | 2024-05-13 |
2654688 | c언어 ㅠㅠ 너 무 어려 워요 ㅠ 도와주세요!! | 별빛 | 2024-05-13 |
2654659 | 구구단 소스 궁금한 점 도움 부탁드립니다! (2) | 미련곰팅이 | 2024-05-12 |
2654633 | 오류가 무었인가요..? (2) | 단순랩퍼 | 2024-05-12 |
2654603 | 어디가 잘못된지를 모르겠습니다. 한 번 봐주세요...ㅠ (2) | 글고운 | 2024-05-12 |
2654574 | 소스 추가좀 부탁드립니다. (등수 출력) | 크리미걸 | 2024-05-12 |
2654550 | 피보나치수열. (3) | 중독적 | 2024-05-11 |