-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSorting_CountSort.java
42 lines (35 loc) · 1018 Bytes
/
Sorting_CountSort.java
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
package com.crossover.demo;
public class Sorting_CountSort {
public static void main(String[] args) {
int[] arr = {1, 4, 1, 2, 7, 5, 2};
for (int elem: arr) {
System.out.print(elem + " ");
}
System.out.println();
countSort(arr);
for (int elem: arr) {
System.out.print(elem + " ");
}
}
public static void countSort(int[] arr) {
int n = arr.length;
int k = arr[0];
for (int i = 0; i < n; i++) {
if (arr[i] > k) k = arr[i];
}
int[] count = new int[k + 1];
for (int i = 0; i < n; i++) {
count[arr[i]]++;
}
for (int i = 1; i <= k; i++) {
count[i] += count[i - 1];
}
int[] b = new int[n];
for (int i = n - 1; i >= 0 ; i--) {
b[--count[arr[i]]] = arr[i];
}
for (int i = 0; i < n; i++) {
arr[i] = b[i];
}
}
}