Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions Sorts/CountingSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,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]+" ");
}

}
}
66 changes: 66 additions & 0 deletions Sorts/CountingSortIntegers.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
*
* @author Youssef Ali (https://github.com/youssefAli11997)
*
*/

class CountingSortIntegers {

/**
* This method implements the Counting Sort for integers
*
* @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 sorts only integer arrays especially positive integers
* It uses array elements as indexes in the frequency array
* It can accept only array elements within the range [0:10^8]
**/

public static void CSI(int array[], int last) {

// The frequency array. It's initialized with zeros
int[] frequency = new int[100000001];
// The final output array
int[] sortedArray = new int[array.length];
int index = 0;

// Counting the frequency of @param array elements
for(int i=0; i<last; i++){
frequency[array[i]]++;
}

// Filling the sortedArray
for(int i=0; i<frequency.length; i++){
for(int j=0; j<frequency[i]; j++)
sortedArray[index++] = i;
}

for(int i=0; i<array.length; i++){
array[i] = sortedArray[i];
}
}

// Driver Program
public static void main(String[] args) {
// Integer Input
int[] 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();

CSI(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();

}
}