GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — leetcode/basic/sorting/CountingSort/CountingSort.java at main · jamesless/leetcode

Expand file tree

/

CountingSort.java

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
38 lines (32 loc) · 963 Bytes
/

CountingSort.java

Copy path

File metadata and controls

38 lines (32 loc) · 963 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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.