From 295ff41346f7a32098ddb08769518c3e7cbb756c Mon Sep 17 00:00:00 2001 From: Youssef Ali Date: Fri, 13 Oct 2017 22:15:31 +0200 Subject: [PATCH 1/3] Create CountingSortIntegers.java --- Sorts/CountingSortIntegers.java | 66 +++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 Sorts/CountingSortIntegers.java diff --git a/Sorts/CountingSortIntegers.java b/Sorts/CountingSortIntegers.java new file mode 100644 index 000000000..b6bb1fde2 --- /dev/null +++ b/Sorts/CountingSortIntegers.java @@ -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 sorted only integer arrays especially positive integers + * It uses array elements as indexes in the frequency array + * 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 1 4 6 9 12 23 54 78 231 + System.out.println("After Sorting:"); + for (int i=0;i Date: Fri, 13 Oct 2017 22:24:36 +0200 Subject: [PATCH 2/3] Update CountingSortIntegers.java --- Sorts/CountingSortIntegers.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sorts/CountingSortIntegers.java b/Sorts/CountingSortIntegers.java index b6bb1fde2..33b6e9094 100644 --- a/Sorts/CountingSortIntegers.java +++ b/Sorts/CountingSortIntegers.java @@ -12,9 +12,9 @@ class CountingSortIntegers { * @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 sorted only integer arrays especially positive integers + * It sorts only integer arrays especially positive integers * It uses array elements as indexes in the frequency array - * Can accept only array elements within the range [0:10^8] + * It can accept only array elements within the range [0:10^8] **/ public static void CSI(int array[], int last) { From 22220d10499cdeb874a6202a6027672da3fff8d7 Mon Sep 17 00:00:00 2001 From: Youssef Ali Date: Sat, 14 Oct 2017 12:52:55 +0200 Subject: [PATCH 3/3] Create CountingSort.java --- Sorts/CountingSort.java | 90 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 Sorts/CountingSort.java diff --git a/Sorts/CountingSort.java b/Sorts/CountingSort.java new file mode 100644 index 000000000..e6828265a --- /dev/null +++ b/Sorts/CountingSort.java @@ -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 > void CS(T[] array, int last) { + + Map frequency = new TreeMap(); + // The final output array + ArrayList sortedArray = new ArrayList(); + + // 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 element : frequency.entrySet()) { + for(int j=0; j 1 4 6 9 12 23 54 78 231 + System.out.println("After Sorting:"); + for (int i=0;i a b c d e + System.out.println("After Sorting:"); + for(int i=0; i