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 — leetcode/basic/sorting/CountingSort/CountingSort.java at main · jamesless/leetcode Expand file tree/ Copy pathCountingSort.javaMore file actions 38 lines (32 loc) · 963 Bytes / Copy pathCountingSort.javaFile metadata and controls38 lines (32 loc) · 963 Bytes 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 35 36 37 38 import java.util.Arrays; public class CountingSort { public static void countingSort(int[] nums, int min, int max) { int n = nums.length; int k = max - min + 1; int[] c = new int[k]; for (int v : nums) { c[v - min]++; } for (int i = 1; i < k; i++) { c[i] += c[i - 1]; } int[] r = new int[n]; for (int i = n - 1; i >= 0; i--) { int v = nums[i]; int a = c[v - min]; r[a - 1] = v; c[v - min]--; } System.arraycopy(r, 0, nums, 0, n); } public static void main(String[] args) { // test case 1 int[] nums = {1, 2, 7, 9, 5, 5, 8}; countingSort(nums, 1, 9); System.out.println(Arrays.toString(nums)); // test case 2 int[] nums2 = {2, 7, 9, 5, 5, 8}; countingSort(nums2, 2, 9); System.out.println(Arrays.toString(nums2)); } } |
| You are visitor 01402775 — 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. |