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
| void sortASC(int[] nums) { int maxValue = nums[0]; int minValue = nums[0]; for (int num : nums) { maxValue = Math.max(maxValue, num); minValue = Math.min(minValue, num); } int offset = 0; if (minValue < 0) { offset = -minValue; maxValue = maxValue + offset; for (int i = 0; i < nums.length; i++) { nums[i] = nums[i] + offset; } } int[] count = new int[maxValue + 1]; for (int num : nums) { count[num]++; } int[] result = new int[nums.length]; int resultIndex = 0; for (int i = 0; i < count.length; i++) { for (int j = 0; j < count[i]; j++) { result[resultIndex++] = i - offset; } } System.arraycopy(result, 0, nums, 0, nums.length); }
|