GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — java-basics/java-programs/CountingSort.java at master · learning-zone/java-basics Expand file tree/ Copy pathCountingSort.javaMore file actions 34 lines (33 loc) · 1.01 KB / Copy pathCountingSort.javaFile metadata and controls34 lines (33 loc) · 1.01 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 package misc; import java.util.Arrays; /** * Counting sort is used to sort elements between specific range. * Counting sort calculates the number of occurrence of objects and stores its key values. * New array is formed by adding previous key elements and assigning to objects. * It can only sort an integer array or number array e.g. short, byte or char array. * */ public class CountingSort { public static void countingSort(int[] input, int k) { int counter[] = new int[k + 1]; // fill bucket for(int i : input) { counter[i]++; } // sort array int ndx = 0; for(int i = 0; i < counter.length; i++) { while( 0 < counter[i]) { input[ndx++] = i; counter[i]--; } } } public static void main(String[] args) { int[] input = { 60, 40, 30, 20, 10, 40, 30, 60, 60, 20, 40, 30, 40 }; int k = 60; System.out.println("Integer array before sorting: " + Arrays.toString(input)); countingSort(input, k); System.out.println("Integer array after sorting: " + Arrays.toString(input)); } } |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |