힙정렬 질문입니다.
튼트나
렌덤 함수 를 줘서 삽입정렬과 힙정렬을 정렬을하고 정렬횟수도 구하는것이에요힙정렬 부분에서 무슨문제인지는 모르겟는데 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;
}
번호 | 제 목 | 글쓴이 | 날짜 |
---|---|---|---|
2692144 | C언어와 리눅스에 대한 질문입니다. | 싴흐한세여니 | 2025-04-20 |
2692114 | 컨텍스트 스위칭하는데 걸리는 시간 측정.. | YourWay | 2025-04-19 |
2692086 | 간접참조 연산자, 증감연산자 질문이용! (2) | 블랙캣 | 2025-04-19 |
2692056 | 주석좀 달아주세요. 몇개적엇는데 몇개만달아주세요. (2) | DevilsTears | 2025-04-19 |
2691978 | 진수 쉽게 이해하는법... (3) | 지지않는 | 2025-04-18 |
2691949 | getchar() 한 문자를 입력받는 함수 질문 | 채꽃 | 2025-04-18 |
2691919 | 배열 정렬 및 합치기 질문입니다. | 사과 | 2025-04-18 |
2691845 | c언어왕초보 질문이 있습니다........ | 루나 | 2025-04-17 |
2691815 | void add(int num); 함수... (4) | 살랑살랑 | 2025-04-17 |
2691756 | 명령 프롬프트 스크롤바가 없어요 | 두메꽃 | 2025-04-16 |
2691725 | 자료구조에 관련해서 질문이 있어 글을 올립니다. | 누리알찬 | 2025-04-16 |
2691697 | if 문에서 구조체 배열에 저장되있던 문자열 검사하는 법 ? (2) | 민트맛사탕 | 2025-04-16 |
2691678 | C언어 함수 질문이요~!!! | 연보라 | 2025-04-15 |
2691650 | 반복문 | 돋가이 | 2025-04-15 |
2691618 | 링크드리스트 개념 질문이예요 (3) | 맨마루 | 2025-04-15 |
2691592 | 동적할당 이용 배열선언 질문입니다.ㅠㅠ (3) | 허리달 | 2025-04-15 |
2691542 | /=의 용도를 알려주세요 ㅠㅠ! (2) | 아라 | 2025-04-14 |
2691510 | sizeof 연산자 질문입니다 (2) | 종달 | 2025-04-14 |
2691483 | 파일 오픈시 에러 질문드립니다. (2) | 호습다 | 2025-04-14 |
2691450 | [visual c++ 툴]기초 질문 (3) | 해긴 | 2025-04-13 |