2021 SISS 21기 활동/여름방학 C언어
[HackerRank] Counting Sort 1
hyerang0125
2021. 8. 5. 22:25
# 문제
# 풀이
int* countingSort(int arr_count, int* arr, int* result_count) {
*result_count = 100;
int *list = malloc(sizeof(int)*(*result_count));
memset(list, 0, sizeof(int)*(*result_count));
for(int i=0; i<arr_count; i++){
list[arr[i]]++;
}
return list;
}
- counting sort는 등장하는 횟수를 세어주는 정렬 방법이다.
- 배열의 크기가 100으로 주어졌으므로, result_count에 100을 넣어주고, 그 크기만큼 반환해줄 배열 list를 만들어 준다.
- 이때 한 번도 등장하지 않는 수가 있을 수 있으므로 모든 배열의 값을 0으로 초기화 해준다.
- arr의 크기만큼 for문을 돌며 해당하는 자리에 값을 증가시켜준다.
- list를 반환하고 프로그램을 종료한다.