forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CountingSort.java
152 lines (138 loc) · 4.53 KB
/
CountingSort.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
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import java.util.*;
import java.lang.*;
/*
Counting sort is an algorithm for sorting a collection of objects according to keys that are small integers; that is, it is an integer sorting algorithm. It operates by counting the number of objects that have each distinct key value, and using arithmetic on those counts to determine the positions of each key value in the output sequence.
Worst complexity: O(n+k)
Average complexity: O(n+k)
Space complexity: O(n+k)
where n is the number of elements in input array and k is the range of input.
*/
class CountingSort
{
static void sortIntegerArray(int arr[])
{
int i=0;
int n = arr.length;
int minElement=Integer.MAX_VALUE;
int maxElement=Integer.MIN_VALUE;
//Maximum and Minimum element from input array
for(i=0;i<n;i++)
{
if (arr[i] > maxElement) {
maxElement=arr[i];
}
if (arr[i] < minElement){
minElement=arr[i];
}
}
int range = maxElement-minElement+1;
int[] arrElementCount = new int[range];
int[] sortedArray = new int[n];
//count occurance of an element
for(i=0;i<n;i++)
{
arrElementCount[arr[i]-minElement]++;
}
//Accumulative sum of elements
for(i=0;i<range-1;i++){
arrElementCount[i+1]+=arrElementCount[i];
}
//Shift array by one index
int temp=arrElementCount[0];
for(i=1;i<range;i++){
int nextTemp=arrElementCount[i];
arrElementCount[i]=temp;
temp=nextTemp;
}
arrElementCount[0]=0;
//store element at right index
for(i=0;i<n;i++){
sortedArray[arrElementCount[arr[i]-minElement]]=arr[i];
arrElementCount[arr[i]-minElement]++;
}
//copy sorted array to input array
for(i=0;i<n;i++){
arr[i]=sortedArray[i];
}
}
static void sortCharArray(char arr[])
{
int i=0;
int n = arr.length;
int range=256;
int[] arrElementCount = new int[range];
char[] sortedArray = new char[n];
//count occurance of an element
for(i=0;i<n;i++)
{
arrElementCount[arr[i]]++;
}
//Accumulative sum of elements
for(i=0;i<range-1;i++){
arrElementCount[i+1]+=arrElementCount[i];
}
//Shift array by one index
int temp=arrElementCount[0];
for(i=1;i<range;i++){
int nextTemp=arrElementCount[i];
arrElementCount[i]=temp;
temp=nextTemp;
}
arrElementCount[0]=0;
//store element at right index
for(i=0;i<n;i++){
sortedArray[arrElementCount[arr[i]]]=arr[i];
arrElementCount[arr[i]]++;
}
//copy sorted array to input array
for(i=0;i<n;i++){
arr[i]=sortedArray[i];
}
}
public static void main (String[] args) throws java.lang.Exception
{
CountingSort sort = new CountingSort();
Scanner sc = new Scanner(System.in);
//Input:- Integer Array
System.out.println("Enter space separated integer:");
String intString[]= sc.nextLine().split(" ");
int size = intString.length;
int[] arrInt = new int[size];
for(int i=0 ;i<size;i++){
arrInt[i]= Integer.parseInt(intString[i]);
}
sort.sortIntegerArray(arrInt);
System.out.println("Sorted Integer Array:- ");
for(int i=0; i<size;i++)
{
System.out.print(arrInt[i]+" ");
}
System.out.println();
//Input:- Character Array
System.out.println("\nEnter space separated character:");
String charString[] = sc.nextLine().split(" ");
int Size=charString.length;
char[] arrChar = new char[Size];
for(int i=0 ;i<Size;i++){
arrChar[i] = charString[i].charAt(0);
}
sort.sortCharArray(arrChar);
System.out.println("Sorted Character Array:- ");
for(int i=0; i<Size;i++)
{
System.out.print(arrChar[i]+" ");
}
System.out.println();
}
}
/*
Sample Input-Output
Enter space separated integer:
7 8 9 4 5 6 7
Sorted Integer Array:-
4 5 6 7 7 8 9
Enter space separated character:
a b t r a b c f
Sorted Character Array:-
a a b b c f r t
*/