Java Program to Implement Counting Sort

This is a Java Program to implement Counting Sort Algorithm. This program is to sort a list of numbers.
Here is the source code of the Java program to implement Counting Sort Algorithm. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

/**
 ** Java Program to Implement Counting Sort
 **/
 
import java.util.Scanner;
 
/** Class CountingSort **/
public class CountingSort 
{
    private static final int MAX_RANGE = 1000000;
    /** Counting Sort function **/
    public static void sort( int[] arr )
    {
        int N = arr.length;
        if (N == 0)
            return;
        /** find max and min values **/
        int max = arr[0], min = arr[0];
        for (int i = 1; i < N; i++)
        {
            if (arr[i] > max)
                max = arr[i];
            if (arr[i] < min)
                min = arr[i];
        }
        int range = max - min + 1;
 
        /** check if range is small enough for count array **/
        /** else it might give out of memory exception while allocating memory for array **/
        if (range > MAX_RANGE)
        {
            System.out.println("\nError : Range too large for sort");
            return;
        }
 
        int[] count = new int[range];
        /** make count/frequency array for each element **/
        for (int i = 0; i < N; i++)
            count[arr[i] - min]++;
        /** modify count so that positions in final array is obtained **/
        for (int i = 1; i < range; i++)
            count[i] += count[i - 1];
        /** modify original array **/
        int j = 0;
        for (int i = 0; i < range; i++)
            while (j < count[i])
                arr[j++] = i + min;
    }    
    /** Main method **/
    public static void main(String[] args) 
    {
        Scanner scan = new Scanner( System.in );        
        System.out.println("Counting Sort Test\n");
        int n, i;
        /** Accept number of elements **/
        System.out.println("Enter number of integer elements");
        n = scan.nextInt();
        /** Create integer array on n elements **/
        int arr[] = new int[ n ];
        /** Accept elements **/
        System.out.println("\nEnter "+ n +" integer elements");
        for (i = 0; i < n; i++)
            arr[i] = scan.nextInt();
        /** Call method sort **/
        sort(arr);
        /** Print sorted Array **/
        System.out.println("\nElements after sorting ");        
        for (i = 0; i < n; i++)
            System.out.print(arr[i]+" ");            
        System.out.println();                     
    }    
}
Counting Sort Test
 
Enter number of integer elements
20
 
Enter 20 integer elements
54 67 13 24 76 37 97 10 67 24 6 28 5 19 63 1 71 83 97 24
 
Elements after sorting
1 5 6 10 13 19 24 24 24 28 37 54 63 67 67 71 76 83 97 97

Related posts:

OAuth2 for a Spring REST API – Handle the Refresh Token in AngularJS
Spring @RequestParam Annotation
Apache Commons Collections MapUtils
Spring @RequestMapping New Shortcut Annotations
Anonymous Classes in Java
Guide to java.util.Formatter
Cơ chế Upcasting và Downcasting trong java
Tạo ứng dụng Java RESTful Client với thư viện Retrofit
Java Program to Implement Self Balancing Binary Search Tree
Hướng dẫn Java Design Pattern – State
Java Program to Implement TreeMap API
Java Program to Implement Hopcroft Algorithm
Java Program to Compute Determinant of a Matrix
Converting a Stack Trace to a String in Java
Request Method Not Supported (405) in Spring
“Stream has already been operated upon or closed” Exception in Java
Thao tác với tập tin và thư mục trong Java
Hướng dẫn Java Design Pattern – Object Pool
Java Program to Implement Ternary Search Tree
Spring Boot Integration Testing with Embedded MongoDB
LinkedHashSet trong java
Java Program to Implement Suffix Array
Custom Error Pages with Spring MVC
Java Program to Implement Expression Tree
Implementing a Runnable vs Extending a Thread
Java Program to Implement Merge Sort on n Numbers Without tail-recursion
How to Read HTTP Headers in Spring REST Controllers
Hướng dẫn Java Design Pattern – Visitor
Java 8 and Infinite Streams
Loại bỏ các phần tử trùng trong một ArrayList như thế nào trong Java 8?
Java Program to Implement ScapeGoat Tree
Java Program to Find Median of Elements where Elements are Stored in 2 Different Arrays