forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountingSort.java
More file actions
57 lines (44 loc) · 1.79 KB
/
CountingSort.java
File metadata and controls
57 lines (44 loc) · 1.79 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
package com.sorts;
public class CountingSort {
/**
* This method sorts the array using counting sort technique
*
* @param arr The array to be sorted
* @return arr Sorted array
*/
public Integer[] sort(Integer[] arr) {
// Finding the maximum element in the array
int max = arr[0];
for (int i = 1; i < arr.length; i++) {
max = Math.max(max, arr[i]);
}
// Finding the minimum element in the array
int min = arr[0];
for (int i = 1; i < arr.length; i++) {
min = Math.min(min, arr[i]);
}
// Creating the count array - This method will store the count of each element in the unsorted array
int[] count = new int[max - min + 1];
// This loop will store the count of each element in the array
for (Integer integer : arr) {
count[integer - min]++;
}
// This loop will replace the ith index of the count array with the sum of values at the ith and (i-1) index
for (int i = 1; i < count.length; i++) {
count[i] = count[i] + count[i - 1];
}
// This array will store the sorted array
Integer[] places = new Integer[arr.length];
// This loop will put the ith element at is correct position in the places array
for (int i = 0; i < places.length; i++) {
// Getting the value of the index - the value at the oount array index will be replaced by the value
// in the original array
int index = arr[i];
places[count[index - min] - 1] = index;
count[index - min]--;
}
// Copy the places array back to the original array - which will be sorted
System.arraycopy(places, 0, arr, 0, arr.length);
return arr;
}
}