Java icon indicating copy to clipboard operation
Java copied to clipboard

Adding a simple implementation of the sorting algorithm which is called "Counting Algorithm".

Open Mohammed-Ryiad-Eiadeh opened this issue 3 years ago • 4 comments

I implemented the counting algorithm in a few lines of code which i think it would be easy to be understood by beginners. And i would like to be accepted and added to your repository.

class CountingSort {

private final int[] OriginalArray;
private final int MaxRange;

/**
 * @param arr is the input array which holds the integer values to be sorted
 * @param Max is the number of digits that the max item in this array has
 */
CountingSort(int[] arr, int Max) {
    this.OriginalArray = arr;
    this.MaxRange = Max;
}

/**
 * sort the numbers using 'counting algorithm'
 * @return the sorted array based on counting algorithm
 */
protected int[] StartSorting() {
    var OccurArray = new int[MaxRange];
    for (var i : OriginalArray)
        OccurArray[i] += 1;

    for (var i = 0; i < OccurArray.length - 1; i++)
        OccurArray[i + 1] += OccurArray[i];

    var SortedArray = new int[OriginalArray.length + 1];
    for (var i : OriginalArray) {
        SortedArray[OccurArray[i]] = i;
        OccurArray[i]--;
    }
    return SortedArray;
}
public static void main(String[] args) {
    // generate 100000 random numbers between 0 and 1000000000
    var arr = new Random().ints(100000, 0, 1000000000).toArray();
    // define a sorting object from counting sort class
    var SortingEngine = new CountingSort(arr, 1000000000);
    // get the start time
    var Start = System.currentTimeMillis();
    // start sorting
    var Result = SortingEngine.StartSorting();
    // get the end time
    var End = System.currentTimeMillis();
    // System.out.println(Arrays.toString(Result));
    // print the duration time of sorting
    System.out.println("time of sorting : " + "\t" + (End - Start) + " ms");
}

}

the output : time of sorting : 2109 ms

Mohammed-Ryiad-Eiadeh avatar Aug 24 '22 13:08 Mohammed-Ryiad-Eiadeh

Please make a PR with counting sort algorithm if it's not implemented yed

siriak avatar Aug 28 '22 05:08 siriak

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

github-actions[bot] avatar Sep 28 '22 00:09 github-actions[bot]

shall i work on this?

ruchikayadav1408 avatar Oct 01 '22 14:10 ruchikayadav1408

can u assign it to me i can make a try on it

rajbhoyar729 avatar Oct 01 '22 19:10 rajbhoyar729

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

github-actions[bot] avatar Jan 28 '23 00:01 github-actions[bot]

Please reopen this issue once you add more information and updates here. If this is not the case and you need some help, feel free to seek help from our Gitter or ping one of the reviewers. Thank you for your contributions!

github-actions[bot] avatar Feb 04 '23 00:02 github-actions[bot]