Created
October 19, 2023 12:40
-
-
Save choonsik/20eca11b3e0d37dd0ad561c3c070492a to your computer and use it in GitHub Desktop.
2000년 어느 날, 직장 선배님께서 요구사항을 주시고 짜달라고 해서 처음 짰던 상용(?) 코드를 찾았다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*춘식이가 회사에서 처음 짜본 프로그램.... | |
별거아닌 프로그램이지만, 처음이기에 긴장했었다... | |
할 수 있어서 다행이다. 히히 | |
*/ | |
#define MAX 10 | |
#include <stdio.h> | |
void initArray(int *arrayName); | |
void array_sort(int *input,int *output,int *ref); | |
void main() | |
{ | |
int i; | |
/* | |
int freqcount[MAX]; | |
int freqsize[MAX]; | |
int result[MAX]; | |
//배열 초기화 | |
initArray(freqcount); | |
initArray(freqsize); | |
initArray(result); | |
*/ | |
int freqcount[MAX]={1,1,2,2,2,1,1,1,1,1}; | |
int freqsize[MAX]={1,2,3,4,5,6,7,8,9,10}; | |
int result[MAX]; | |
array_sort(freqcount,result,freqsize); | |
for(i=0;i<MAX;i++) | |
printf("result[%d]=%d\n",i,result[i]); | |
} | |
void initArray(int *arrayName) | |
{ | |
int i; | |
for(i=0;i<MAX;i++){ | |
arrayName[i]=0; | |
} | |
} | |
void array_sort(int *input,int *output,int *ref) | |
{ | |
int sort[2][MAX]; //0행은 value,1행은 index; | |
int i,j; | |
int temp; | |
int indextemp; | |
//input value가 같을 때, 비교를 위해 | |
int sortIndex; | |
int sortIndex2; | |
//input배열 복사 | |
for(i=0;i<MAX;i++){ | |
sort[0][i]=input[i]; | |
sort[1][i]=i; | |
} | |
//버블 소트 사용 | |
for(i=MAX-1;i>=0;--i){ | |
for(j=0;j<i;++j){ | |
if(sort[0][j]<sort[0][j+1]){ | |
temp=sort[0][j+1]; | |
sort[0][j+1]=sort[0][j]; | |
sort[0][j]=temp; | |
indextemp=sort[1][j+1]; | |
sort[1][j+1]=sort[1][j]; | |
sort[1][j]=indextemp; | |
} | |
//동일할 경우 ref 배열로 판단, index 만 변경 | |
if(sort[0][j]==sort[0][j+1]){ | |
//sort배열의 인덱스를 가지고 비교해야 지요! 찾았다. | |
sortIndex=sort[1][j]; | |
sortIndex2=sort[1][j+1]; | |
if(ref[sortIndex]<ref[sortIndex2]){ | |
indextemp=sort[1][j+1]; | |
sort[1][j+1]=sort[1][j]; | |
sort[1][j]=indextemp; | |
} | |
} | |
} | |
} | |
//소트된 것을 output으로 | |
for(i=0;i<MAX;i++) | |
output[i]=sort[1][i]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
코드를 보고 한참 웃었다!