algorithm-base/animation-simulation/数据结构和算法/计数排序.md

234 lines
11 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#
> **[tan45du_one](https://raw.githubusercontent.com/tan45du/tan45du.github.io/master/个人微信.15egrcgqd94w.jpg)** ,备注 github + 题目 + 问题 向我反馈
>
>
>
> <u>[****](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u> 两个平台同步,想要和题友一起刷题,互相监督的同学,可以在我的小屋点击<u>[**刷题小队**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u>进入。
线
100000 10 0 - 10
线
O(nlogn) ,
On线线
Ο(n+k) k
10
1235022459
10
![](https://cdn.jsdelivr.net/gh/tan45du/photobed@master/微信截图_20210327184632.ho3d12nf3q8.png)
9 9 + 1 = 10
****
0 1 1 1 2 3
0122234559
![_20210327202256](https://cdn.jsdelivr.net/gh/tan45du/photobed@master/微信截图_20210327202256.7g3nka7n0p40.png)
4
![](https://cdn.jsdelivr.net/gh/tan45du/photobed@master/微信截图_20210328131226.3x42hsrnna80.png)
presum
presum[2] = 5 , 2 5 presum[4] = 7 4 7
presum
![_20210328132549](https://cdn.jsdelivr.net/gh/tan45du/photobed@master/微信截图_20210328132549.4w6kovlhtsa0.png)
nums[9] = 9, presum presum[nums[9]] = presum[9] = 10 presum presum[9] = 10, 10 10 temp[9] = 9
9 temp presum 1 presum[9] = 10 - 1 = 9;
![](https://cdn.jsdelivr.net/gh/tan45du/photobed@master/微信截图_20210328133100.5h5w473oi2s0.png)
5
![](https://cdn.jsdelivr.net/gh/tan45du/photobed@master/微信截图_20210328133401.23fulpjowbnk.png)
presum presum[5] = 9, 5 9 temp 9
temp[8] = 5 presum[5] 1
![](https://cdn.jsdelivr.net/gh/tan45du/photobed@master/微信截图_20210328133726.3s8wgrlcpzm0.png)
presum
bug
presum
![](https://cdn.jsdelivr.net/gh/tan45du/photobed@master/计数排序.6y4quuwtxgw0.gif)
9093949192 presum 9594
nums[index] presum nums[index] presum
9093949192 max min 94 - 90 + 1 = 5 min 90
![](https://cdn.jsdelivr.net/gh/tan45du/photobed@master/微信截图_20210328153724.61tvwfwjmmo0.png)
presum
-1-3021
![](https://cdn.jsdelivr.net/gh/tan45du/photobed@master/微信截图_20210328154337.qtnhiuaixzk.png)
Java Code:
```java
class Solution {
public int[] sortArray(int[] nums) {
int len = nums.length;
if (nums.length < 1) {
return nums;
}
//求出最大最小值
int max = nums[0];
int min = nums[0];
for (int x : nums) {
if (max < x) max = x;
if (min > x) min = x;
}
//设置 presum 数组长度,然后求出我们的前缀和数组,
//这里我们可以把求次数数组和前缀和数组用一个数组处理
int[] presum = new int[max-min+1];
for (int x : nums) {
presum[x-min]++;
}
for (int i = 1; i < presum.length; ++i) {
presum[i] = presum[i-1]+presum[i];
}
//临时数组
int[] temp = new int[len];
//遍历数组,开始排序,注意偏移量
for (int i = len-1; i >= 0; --i) {
//查找 presum 字典,然后将其放到临时数组,注意偏移度
int index = presum[nums[i]-min]-1;
temp[index] = nums[i];
//相应位置减一
presum[nums[i]-min]--;
}
//copy回原数组
System.arraycopy(temp,0,nums,0,len);
return nums;
}
}
```
Python Code:
```python
from typing import List
class Solution:
def sortArray(self,nums: List[int])->List[int]:
leng = len(nums)
if leng < 1:
return nums
#
max = nums[0]
min = nums[0]
for x in nums:
if max < x:
max = x
if min > x:
min = x
# presum ,
#
presum = [0] * (max - min + 1)
for x in nums:
presum[x - min] += 1
for i in range(1, len(presum)):
presum[i] = presum[i - 1] + presum[i]
#
temp = [0] * leng
#
for i in range(leng - 1, -1, -1):
# presum
index = presum[nums[i] - min] - 1
temp[index] = nums[i]
#
presum[nums[i] - min] -= 1
# copy
nums = temp
return nums
```
****
n+n+k+n 3n + k O(N+K)
****
On
****
-
- ,,?
,,,.