forked from AllAlgorithms/java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountingSort.java
More file actions
42 lines (36 loc) · 1004 Bytes
/
CountingSort.java
File metadata and controls
42 lines (36 loc) · 1004 Bytes
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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author miqdad
*/
public class CountingSort {
static int[] countingSort(int[] nums, int maxNumber) {
int[] temp = new int[maxNumber + 1];
int[] result = new int[nums.length];
for (int i = 0; i < nums.length; i++) {
temp[nums[i]]++;
}
int index = 0;
for (int i = 0; i < temp.length; i++) {
while (temp[i] > 0) {
result[index++] = i;
temp[i]--;
}
}
return result;
}
static void printArray(int[] nums) {
for (int num : nums) {
System.out.println(num);
}
}
public static void main(String[] args) {
int[] nums = { 3, 2, 5, 6, 7, 1, 9, 0, 8, 6 };
nums = countingSort(nums, 10);
printArray(nums);
}
}