티스토리 뷰

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();
}

'algorithm' 카테고리의 다른 글

[입출력] 10953, 11021, 11718  (0) 2020.04.07
[입출력] 1000, 2558, 10950  (0) 2020.04.06
[Algorithm] 다이나믹 프로그래밍(Dynamic programming)  (0) 2020.03.30
HeapSort with java  (0) 2020.01.24
MergeSort with java  (0) 2020.01.24
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/07   »
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
글 보관함