radix sort 시간단축방법좀..
누림
질문 제목 : radix sort 시간단축방법좀..
자료구조 과제로 radix 소트를 이용한 프로그램을 만드는건데요 한 레코드에는 세개의 필드가 들어가고 필드의 우선순위는 코드에서보면 abc 순입니다. 과제 내용도 참 복잡한데.. 예를들면
5 //레코드의수
1 900 30 40 300 60 35 10 500 20 300 1004 35 10 405
// 1번레코드a 1번레코드b 1번레코드c 2번레코드a 2번레코드b 2번레코드c ... 이런식으로 입력되고
1 900 30 20 300 1004 35 10 405 35 10 500 40 300 60 // 이런식으로 출력됩니다.
정렬이 되긴 되는데 시간이 너무오래걸려서 점수를 못받게생겼네요..
질문 내용 :
코드입니다
#includestdio.h
#includememory.h
#includestdlib.h
typedef struct in_type{
unsigned int a;
unsigned int b;
unsigned int c;
struct in_type *next;
}in_type;
in_type head[4096], tail[4096];
in_type buffer[124];
void radix_sort(in_type *buffer, int num)
{
int i,j,k;
in_type bucket[4097];
in_type *tmp;
in_type *temp;
in_type *free;
int num_bucket[4097]={0};
memset(bucket,0,sizeof(in_type)*4097);
for(i=0; inum ;i++)
{
tmp=(in_type*)malloc(sizeof(in_type));
memcpy(tmp,buffer+i,sizeof(in_type));
if(!num_bucket[tmp-c])
{
head[tmp-c].next=tmp;
bucket[tmp-c]=head[tmp-c];
}
else
{
temp=&head[tmp-c];
while(1)
{
if( temp-a == temp - next- a && temp-b == temp- next- b && temp-c==temp-next-c )
break;
temp=temp-next;
}
temp-next=tmp;
}
(num_bucket[tmp-c])++;
}
for(i=0,j=0; i4096 ; i++)
{
if(num_bucket[i])
{
k=0;
while(1)
{
memcpy(buffer+j,bucket[i].next,sizeof(in_type));
buffer[j].next=&buffer[j];
j++;
free=bucket[i].next-next;
bucket[i].next=bucket[i].next-next;
num_bucket[i]--;
if(!num_bucket[i])
break;
}
}
}
memset(bucket,0,sizeof(in_type)*4097);
memset(num_bucket,0,4097*sizeof(int));
//버켓 //버켓 넘버 초기화
for(i=0; inum ;i++)
{
tmp=(in_type*)malloc(sizeof(in_type));
memcpy(tmp,buffer+i,sizeof(in_type));
if(!num_bucket[tmp-b])
{
head[tmp-b].next=tmp;
bucket[tmp-b]=head[tmp-b];
}
else
{
temp=&head[tmp-b];
while(1)
{
if(temp-a==temp-next-a && temp-b==temp-next-b&& temp-c==temp-next-c)
break;
temp=temp-next;
}
temp-next=tmp;
}
(num_bucket[tmp-b])++;
}
for(i=0,j=0; i4096 ; i++)
{
if(num_bucket[i])
{
k=0;
while(1)
{
memcpy(buffer+j,bucket[i].next,sizeof(in_type));
buffer[j].next=&buffer[j];
j++;
free=bucket[i].next-next;
bucket[i].next=bucket[i].next-next;
num_bucket[i]--;
if(!num_bucket[i])
break;
}
}
}
//버켓 //버켓 넘버 초기화
memset(bucket,0,sizeof(in_type)*4097);
memset(num_bucket,0,4097*sizeof(int));
for(i=0; inum ;i++)
{
tmp=(in_type*)malloc(sizeof(in_type));
memcpy(tmp,buffer+i,sizeof(in_type));
if(!num_bucket[tmp-a])
{
head[tmp-a].next=tmp;
bucket[tmp-a]=head[tmp-a];
}
else
{
temp=&head[tmp-a];
while(1)
{
if(temp-a==temp-next-a && temp-b==temp-next-b&& temp-c==temp-next-c)
break;
temp=temp-next;
}
temp-next=tmp;
}
(num_bucket[tmp-a])++;
}
for(i=0,j=0; i4096 ; i++)
{
if(num_bucket[i])
{
k=0;
while(1)
{
memcpy(buffer+j,bucket[i].next,sizeof(in_type));
buffer[j].next=&buffer[j];
j++;
free=bucket[i].next-next;
bucket[i].next=bucket[i].next-next;
p;num_bucket[i]--;
if(!num_bucket[i])
break;
}
}
}
}
int main(int argc, char *argv[])
{
int num, i;
scanf(%d, &num);
while(num != -1)
{
for(i = 0; i num; i++)
{
scanf(%d, &buffer[i].a);
scanf(%d, &buffer[i].b);
scanf(%d, &buffer[i].c);
buffer[i].next=&buffer[i];
}
radix_sort(buffer, num);
for(i = 0; i num; i++)
{
printf(%d , buffer[i].a);
printf(%d , buffer[i].b);
printf(%d , buffer[i].c);
}
printf(\n);
scanf(%d, &num);
}
return 0;
}
제가 코딩실력이 모자라서 코드가 좀 엉망이네요..
이상태에서 시간 조금이라도 단축하려면 어떻게 해야하나요 ㅠ
번호 | 제 목 | 글쓴이 | 날짜 |
---|---|---|---|
2692282 | 도스상에서 생성된 exe파일에 press~ 뜨게 하기 (4) | 회사원 | 2025-04-21 |
2692256 | scanf("%*c"); ㅠㅠ 고수님들 | 거북이 | 2025-04-21 |
2692230 | 하노이탑 질문입니다. (1) | 미쁘다 | 2025-04-21 |
2692210 | 정보 올림피아드 문제인데.. 풀이 과정이 궁금합니다.(재귀함수) (5) | 물티슈 | 2025-04-20 |
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 |