본문 바로가기

Programming Skill/Data Structure & Algorithm

[알고리즘] 간접정렬(Indirect Sort)

작업 환경

OS : Ubuntu 14.04.5 LTS


간접정렬이란?

- 인덱스 기법이라고도 하며 레코드 배열에서 데이터를 정렬하는 것이 아니라 인덱스 배열을 따로 만들어 정렬하는 기법

- 교환횟수가 많은 삽입정렬의 단점을 보완했으며, 간단한 정수를 정렬할 때 주로 사용


문제

배열에 10,2,9,1,4,8,6,7,5,3 값 10개를 넣은뒤 간접정렬을 사용하여 출력하시오.


indirect_sort.c

#include <stdio.h>


int main()

{

int i, j, cnt;

char buf[10] = { 10, 2, 9, 1, 4, 8, 6, 7, 5, 3 };

char index[10];


for (i = 0; i<10; ++i)

{

cnt = 0;

for (j = 1; j<10; ++j)

{

if (buf[i] > buf[j])

{

cnt++;

}

}

index[cnt] = i;

}


for (i = 0; i<10; ++i)

{

printf("%d ", buf[index[i]]);

}


putchar('\n');


return 0;

}


컴파일 : gcc indirect_sort.c -o indirect_sort

실행 : ./indirect_sort


실행결과