Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
90 lines (72 loc) · 2.36 KB

File metadata and controls

90 lines (72 loc) · 2.36 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
import java.util.ArrayList;
import java.util.Map;
import java.util.TreeMap;
/**
*
* @author Youssef Ali (https://github.com/youssefAli11997)
*
*/
class CountingSort {
/**
* This method implements the Generic Counting Sort
*
* @param array The array to be sorted
* @param last The count of total number of elements in array
* Sorts the array in increasing order
* It uses array elements as keys in the frequency map
**/
public static <T extends Comparable<T>> void CS(T[] array, int last) {
Map<T, Integer> frequency = new TreeMap<T, Integer>();
// The final output array
ArrayList<T> sortedArray = new ArrayList<T>();
// Counting the frequency of @param array elements
for(T t : array) {
try{
frequency.put(t, frequency.get(t)+1);
}catch(Exception e){ // new entry
frequency.put(t, 1);
}
}
// Filling the sortedArray
for(Map.Entry<T, Integer> element : frequency.entrySet()) {
for(int j=0; j<element.getValue(); j++)
sortedArray.add(element.getKey());
}
for(int i=0; i<array.length; i++){
array[i] = sortedArray.get(i);
}
}
// Driver Program
public static void main(String[] args) {
// Integer Input
Integer[] arr1 = {4,23,6,78,1,54,231,9,12};
int last = arr1.length;
System.out.println("Before Sorting:");
for (int i=0;i<arr1.length;i++) {
System.out.print(arr1[i] + " ");
}
System.out.println();
CS(arr1, last);
// Output => 1 4 6 9 12 23 54 78 231
System.out.println("After Sorting:");
for (int i=0;i<arr1.length;i++) {
System.out.print(arr1[i] + " ");
}
System.out.println();
System.out.println("------------------------------");
// String Input
String[] array1 = {"c", "a", "e", "b","d"};
last = array1.length;
System.out.println("Before Sorting:");
for (int i=0;i<array1.length;i++) {
System.out.print(array1[i] + " ");
}
System.out.println();
CS(array1, last);
//Output => a b c d e
System.out.println("After Sorting:");
for(int i=0; i<last; i++) {
System.out.print(array1[i]+" ");
}
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.