Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- redhat
- 재귀
- Redshift
- big data
- 스토리지
- 자료구조
- data
- 스프링
- 리눅스
- docker
- 레드햇
- 빅데이터
- 알고리즘
- 도커
- recursive
- algorithm
- Amazon
- hadoop
- 아마존
- 자바
- linux
- 하둡
- rhcsa
- AWS
- storage
- Data Structure
- 설치
- sort
- Spring
- java
Archives
- Today
- Total
Developer MJ
[Java] 계수정렬(Counting Sort) 본문
집합에 각 항목이 몇 개씩 있는지 세는 작업을 하면서 정렬
import java.util.Scanner;
class Solution {
static final int MAX_N = 100;
static final int MAX_DIGIT = 10;
static int N; // # of data set
static int arr[];
static int cnt[] = new int[MAX_DIGIT];
static int sortedArr[];
static void calculateDigitNumber()
{
for (int i = 0; i < N; i++)
{
cnt[arr[i]]++;
}
for (int i = 1; i < MAX_DIGIT; i++)
{
cnt[i] = cnt[i-1] + cnt[i];
}
}
static void executeCountingSort()
{
for (int i = N-1; i >= 0; i--)
{
sortedArr[--cnt[arr[i]]] = arr[i];
}
}
public static void main(String arg[]) throws Exception
{
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
for (int test_case = 1; test_case <= T; test_case++)
{
N = sc.nextInt();
arr = new int[N];
sortedArr = new int[N];
for (int i = 0; i < N; i++)
{
arr[i] = sc.nextInt();
}
// initialize
for (int i = 1; i < MAX_DIGIT; i++)
{
cnt[i] = 0;
}
calculateDigitNumber();
executeCountingSort();
//print the sorted digits
System.out.print("#" + test_case + " ");
for (int i = 0; i < N; i++)
{
System.out.print(sortedArr[i] + " ");
}
System.out.println();
}
sc.close();
}
}
'Programming > Code' 카테고리의 다른 글
[C/C++] Binary Search Tree 구현 (0) | 2019.04.24 |
---|---|
[Java] 퀵정렬 (Quick Sort) (0) | 2019.04.18 |
[Java] 삽입 정렬 (Insertion Sort) (0) | 2019.04.15 |
[Java] Queue (0) | 2019.02.13 |
[Java] Stack (0) | 2019.02.13 |