algorithm
CountingSort with java
tonirr
2020. 1. 24. 15:12
- 범위조건이 있는 경우에 한해서 굉장히 빠른 알고리즘
- 속도는 O(N)
- 단순하게 크기를 기준으로 세는 알고리즘임
public class CountingSort {
int temp;
public static void counting() {
int []count = new int[5];
int array[] = {
1,3,2,4,3,
2,5,3,1,2,
3,4,4,3,5,
1,2,3,5,2,
3,1,4,3,5,
1,2,1,1,1};
for(int i = 0; i < 5; i++) {
count[i] = 0;
}
for(int i = 0; i < 30; i++) {
count[array[i] - 1]++;
}
for(int i = 0; i < 5; i++) {
if(count[i] != 0) {
for(int j = 0; j < count[i]; j++) {
System.out.println((i+1)+" ");
}
}
}
}
public static void main(String[] args) {
counting();
}