-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCounting_Sort.java
More file actions
78 lines (55 loc) · 1.37 KB
/
Counting_Sort.java
File metadata and controls
78 lines (55 loc) · 1.37 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package sort.countingSort;
//Counting_Sort 클래스
public class Counting_Sort {
private static void countingSort(int[] arr) {
int max = arr[0];
//입력된 배열중 가장 큰 값 찾기
for(int i=0; i<arr.length; i++) {
if(max < arr[i]) {
max = arr[i];
}
}
max ++;
//counting 배열 선언 및 생성
int[] c = new int[max];
//정렬된 배열 선언 및 생성
int[] b = new int[arr.length];
//counting 배열 초기화
for(int i=0; i<max; i++) {
c[i] = 0;
}
//입력된 배열의 값 카운팅 값 넣기
for(int i=0; i<arr.length; i++) {
c[arr[i]] = c[arr[i]] + 1;
}
// c배열의 값 누적하기
for(int i=1; i<max; i++) {
c[i] = c[i] + c[i-1];
}
//계수 정렬
for(int i=(arr.length-1); i>=0; i--) {
b[c[arr[i]]-1] = arr[i];
c[arr[i]] = c[arr[i]] - 1;
}
//원래 배열에 정렬된 배열을 덮어쓰기
for(int i=0; i<arr.length; i++) {
arr[i] = b[i];
}
}
//printArray: 배열을 출력하는 메서드
private static void printArray(int[] arr) {
for(int data : arr) {
System.out.print(data+",");
}
System.out.println();
}
//main 메서드
public static void main(String[] args) {
int[] arr = {2,5,3,0,2,3,0,3};
System.out.println("===========정렬 전============");
printArray(arr);
System.out.println("===========정렬 후============");
countingSort(arr);
printArray(arr);
}//main 종료
}//Counting_Sort 종료