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

338 lines
15 KiB
Java
Raw Normal View History

2021-03-20 07:58:25 +00:00
### ****
2021-03-21 05:13:12 +00:00
> **[tan45du_one](https://raw.githubusercontent.com/tan45du/tan45du.github.io/master/个人微信.15egrcgqd94w.jpg)** ,备注 github + 题目 + 问题 向我反馈
>
>
>
2021-03-21 05:13:12 +00:00
> <u>[****](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u> 两个平台同步,想要和题友一起刷题,互相监督的同学,可以在我的小屋点击<u>[**刷题小队**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u>进入。
2021-03-20 07:58:25 +00:00
![_20210316124303](https://cdn.jsdelivr.net/gh/tan45du/test@master/photo/微信图片_20210316124303.1lo4nr3xhrwg.jpg)
1423
1
2
![](https://cdn.jsdelivr.net/gh/tan45du/test@master/photo/微信截图_20210223221833.6slujxq1cb40.png)
1
2
1
![](https://cdn.jsdelivr.net/gh/tan45du/test@master/photo/微信截图_20210223223621.3juf4t4hc9a0.png)
1 1 , 4 2 2 3
i , ** 2*i** 0 2*i+1 , 2*i+1**** i/2 **** ****
-
-
使
****
![](https://img-blog.csdnimg.cn/20210317193005203.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzMzODg1OTI0,size_16,color_FFFFFF,t_70#pic_center)
1 绿 1 7 1 1 7
i/2
![](https://img-blog.csdnimg.cn/20210317192914891.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzMzODg1OTI0,size_16,color_FFFFFF,t_70#pic_center)
![](https://img-blog.csdnimg.cn/20210317192922435.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzMzODg1OTI0,size_16,color_FFFFFF,t_70#pic_center)
1 1
![](https://img-blog.csdnimg.cn/20210317193205782.gif#pic_center)
****
Java Code:
2021-03-20 07:58:25 +00:00
```java
public void swim (int[] nums, int index) {
2021-03-20 07:58:25 +00:00
while (index > 1 && nums[index/2] > nums[index]) {
swap(index/2,index);//交换
index = index/2;
}
}
```
Python Code:
```python
def swim(nums: int, index: int):
while index > 1 and nums[int(index/2)] > nums[index]:
swap(int(index/2), index)#
index = int(index/2)
```
2021-03-20 07:58:25 +00:00
![](https://cdn.jsdelivr.net/gh/tan45du/test@master/photo/微信截图_20210309143155.2fhvnp8lqe4g.png)
7 7
![](https://img-blog.csdnimg.cn/20210317193217911.gif#pic_center)
7 2 3
![](https://cdn.jsdelivr.net/gh/tan45du/test@master/photo/微信截图_20210309145953.1byz4zq0cx6o.png)
7 5
- 6
- 7
> ** [8,5,7,9,2,10,1,4,6,3]**
![](https://img-blog.csdnimg.cn/20210317193229153.gif#pic_center)
Java Code:
2021-03-20 07:58:25 +00:00
```java
public void sink (int[] nums, int index,int len) {
while (true) {
//获取子节点
int j = 2 * index;
if (j < len-1 && nums[j] < nums[j+1]) {
j++;
}
//交换操作,父节点下沉,与最大的孩子节点交换
if (j < len && nums[index] < nums[j]) {
swap(nums,index,j);
} else {
break;
}
//继续下沉
index = j;
}
}
```
Python Code:
```python
def sink(nums: list, index: int, len: int):
while True:
#
j = 2 * index
if j < len-1 and nums[j] < nums[j+1]:
j += 1
#
if j < len and nums[index] < nums[j]:
swap(nums, index, j)
else:
break
#
index = j
```
2021-03-20 07:58:25 +00:00
![](https://cdn.jsdelivr.net/gh/tan45du/test@master/photo/微信截图_20210309200153.3jx6dvweliq0.png)
11 2 2
![](https://cdn.jsdelivr.net/gh/tan45du/test@master/photo/微信截图_20210309200301.5zqydpf44mo0.png)
![](https://img-blog.csdnimg.cn/20210317193246980.gif#pic_center)
1.
2.
Java Code:
2021-03-20 07:58:25 +00:00
```java
class Solution {
public int[] sortArray(int[] nums) {
int len = nums.length;
int[] a = new int[len + 1];
for (int i = 0; i < nums.length; ++i) {
a[i+1] = nums[i];
}
//下沉建堆
for (int i = len/2; i >= 1; --i) {
sink(a,i,len);
}
int k = len;
//排序
while (k > 1) {
swap(a,1,k--);
sink(a,1,k);
}
for (int i = 1; i < len+1; ++i) {
nums[i-1] = a[i];
}
return nums;
}
public void sink (int[] nums, int k,int end) {
//下沉
while (2 * k <= end) {
int j = 2 * k;
//找出子节点中最大或最小的那个
if (j + 1 <= end && nums[j + 1] > nums[j]) {
j++;
}
if (nums[j] > nums[k]) {
swap(nums, j, k);
} else {
break;
}
k = j;
}
}
public void swap (int nums[], int i, int j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
```
Python Code:
```python
def sortArray(nums: list)->list:
leng = len(nums)
a = [0] + nums
#
for i in range(int(leng / 2), 0, -1):
sink(a, i, leng)
k = leng
#
while k > 1:
swap(a, 1, k)
k -= 1
sink(a, 1, k)
for i in range(1, leng + 1):
nums[i - 1] = a[i]
return nums
def swap(nums: list, i: int, j: int):
temp = nums[i]
nums[i] = nums[j]
nums[j] = temp
def sink(nums: list, k: int, end: int):
while 2 * k <= end:
j = 2 * k
if j + 1 <= end and nums[j + 1] > nums[j]:
j += 1
if nums[j] > nums[k]:
swap(nums, j, k)
else:
break
k = j
```
2021-03-20 07:58:25 +00:00
广
****
O(n O(nlogn), O(nlogn)
****
i * 2 i * 2+1 i * 2 + 1 i * 2 + 2 O(1),
****
1.访访 CPU
2.
2021-03-20 08:30:29 +00:00