Java Program to Perform the Sorting Using Counting Sort

«
»
This is a java program to sort the numbers using the Counting Sort Technique. In computer science, counting sort is an algorithm for sorting a collection of objects according to keys that are small integers; that is, it is an integer sorting algorithm. It operates by counting the number of objects that have each distinct key value, and using arithmetic on those counts to determine the positions of each key value in the output sequence. Its running time is linear in the number of items and the difference between the maximum and minimum key values.

Here is the source code of the Java Program to Perform the Sorting Using Counting Sort. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. //This is a java program to sort numbers using counting sort
  2. import java.util.Random;
  3.  
  4. public class Counting_Sort 
  5. {
  6.     public static int N = 20;
  7.     public static int[] sequence = new int[N];
  8.     private static final int MAX_RANGE = 1000000;
  9.  
  10.     public static void sort(int[] arr) 
  11.     {
  12.         int N = arr.length;
  13.         if (N == 0)
  14.             return;
  15.         int max = arr[0], min = arr[0];
  16.         for (int i = 1; i < N; i++) 
  17.         {
  18.             if (arr[i] > max)
  19.                 max = arr[i];
  20.             if (arr[i] < min)
  21.                 min = arr[i];
  22.         }
  23.         int range = max - min + 1;
  24.  
  25.         if (range > MAX_RANGE) 
  26.         {
  27.             System.out.println("\nError : Range too large for sort");
  28.             return;
  29.         }
  30.  
  31.         int[] count = new int[range];
  32.         for (int i = 0; i < N; i++)
  33.             count[arr[i] - min]++;
  34.         for (int i = 1; i < range; i++)
  35.             count[i] += count[i - 1];
  36.         int j = 0;
  37.         for (int i = 0; i < range; i++)
  38.             while (j < count[i])
  39.                 arr[j++] = i + min;
  40.     }
  41.  
  42.     public static void main(String[] args) 
  43.     {
  44.         System.out.println("Counting Sort Test\n");
  45.         Random random = new Random();
  46.  
  47.         for (int i = 0; i < N; i++)
  48.             sequence[i] = Math.abs(random.nextInt(100));
  49.  
  50.         System.out.println("Elements before sorting");
  51.         for (int i = 0; i < N; i++)
  52.             System.out.print(sequence[i] + " ");
  53.         System.out.println();
  54.  
  55.         sort(sequence);
  56.  
  57.         System.out.println("\nElements after sorting ");
  58.         for (int i = 0; i < N; i++)
  59.             System.out.print(sequence[i] + " ");
  60.         System.out.println();
  61.     }
  62. }

Output:

advertisement
$ javac Counting_Sort
$ java Counting_Sort
 
Counting Sort
 
Elements before sorting
7 7 41 56 91 65 86 84 70 44 90 38 78 58 34 87 56 16 23 86 
 
Elements after sorting 
7 7 16 23 34 38 41 44 56 56 58 65 70 78 84 86 86 87 90 91

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement

Here’s the list of Best Reference Books in Java Programming, Data Structures and Algorithms.

Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!
advertisement
advertisement
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Youtube | Instagram | Facebook | Twitter