Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

How to perform counting sort in Java?

import java.util.Arrays;

public class CountingSort {
	/*
	 * This code performs a Counting sort of
	 * a list of positive integer values.
	 * 
	 * Let n be size of the list to be sorted and
	 * m be the maximum value in the list
	 * 
	 * Time complexity: O(n)
	 * Space complexity: O(n+m)
	 * 
	 */
	public static void main(String[] args) {
		int[] arr = { 2, 4, 3, 1 };

		System.out.println(Arrays.toString(arr)); // [2, 4, 3, 1]
		int[] sortedArr = countingSort(arr);
		System.out.println(Arrays.toString(sortedArr)); // [1, 2, 3, 4]
	}
	private static int[] countingSort(int[] arr) {
		/*
		 * The counting sort method counts the number
		 * of occurrences of every element in arr.
		 * Then uses that information to sort these elements.
		 */
		int max = findMax(arr);
		if (max == Integer.MIN_VALUE) {
			return new int[] {};
		}
		int[] counts = new int[max + 1];
		for (int num : arr) {
			counts[num]++;
		}
		for (int idx = 1; idx < counts.length; idx++) {
			counts[idx] = counts[idx - 1] + counts[idx];
		}
		int[] sortedArray = new int[arr.length];
		int current, sortedIdx;
		for (int idx = arr.length - 1; idx >= 0; idx--) {
			current = arr[idx];
			counts[current]--;
			sortedIdx = counts[current];
			sortedArray[sortedIdx] = current;
		}
		return sortedArray;
	}
	private static int findMax(int[] arr) {
		int max = Integer.MIN_VALUE;
		for (int num : arr) {
			if (num > max) {
				max = num;
			}
		}
		return max;
		// or return Collections.max(Arrays.asList(arr));
	}
}
Comment

counting sort java

public static void CountingSort(int A[], int k) {//k is equal to A.length
        int[] B = new int[k + 1];
        int max = A[0];
        for (int i = 1; i < k; i++) {
            if (A[i] > max)
                max = A[i];
        }
        int[] C = new int[max + 1];
        for (int i = 0; i < max; ++i) {
            C[i] = 0;
        }
        for (int i = 0; i < k; i++) {
            C[A[i]]++;
        }
        for (int i = 1; i <= max; i++) {
            C[i] += C[i - 1];
        }
        for (int i = k - 1; i >= 0; i--) {
            B[C[A[i]] - 1] = A[i];
            C[A[i]]--;
        }
        for (int i = 0; i < k; i++) {
            A[i] = B[i];
        }
    }
Comment

PREVIOUS NEXT
Code Example
Java :: iterator for array java 
Java :: slice array java 
Java :: print queue in java 
Java :: open camera in android 
Java :: how to add minutes in date in java 
Java :: Service vs Intent Service 
Java :: java stack 
Java :: java sout two dimensional array 
Java :: convert int array to Integer list java 
Java :: get month from int java 
Java :: {} when initialising arraylist java 
Java :: char default value in java 
Java :: recyclerview stop scrolling 
Java :: try catch out of bounds exception java 
Java :: Java Hashmap Access Elements 
Java :: java sort list of strings 
Java :: @data lombok 
Java :: java add com.google.guava dependancy maven 
Java :: full screen dialog android 
Java :: how to enable cors request on springboot application 
Java :: why java platform independent 
Java :: selenium java wait for page load 
Java :: install java debian 8 
Java :: java isolate the numbers from string 
Java :: change number into array in java 
Java :: java print hello world 
Java :: java get year 
Java :: if number is negative java 
Java :: 12 hours to 24 hours java 
Java :: java mp3 player 
ADD CONTENT
Topic
Content
Source link
Name
7+2 =