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

336 lines
15 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>进入。
![_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:
```java
public void swim (int[] nums, int index) {
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)
```
![](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:
```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
```
![](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:
```java
public class Solution {
int rollBack(int i) {
return i - 1;
}
public int[] sortArray(int[] nums) {
int len = nums.length;
//下沉建堆
for (int i = len / 2; i >= 1; --i) {
sink(nums, i, len);
}
int k = len;
//排序
while (k > 1) {
swap(nums, 1, k--);
sink(nums, 1, k);
}
return nums;
}
public void sink(int[] nums, int k, int end) {
//下沉
while (2 * k <= end) {
int j = 2 * k;
//找出子节点中最大或最小的那个
if (j + 1 <= end && nums[rollBack(j + 1)] > nums[rollBack(j)]) {
j++;
}
if (nums[rollBack(j)] > nums[rollBack(k)]) {
swap(nums, j, k);
} else {
break;
}
k = j;
}
}
void swap(int[] nums, int i, int j) {
int temp = nums[rollBack(i)];
nums[rollBack(i)] = nums[rollBack(j)];
nums[rollBack(j)] = temp;
}
}
```
Python Code:
```python
def rollBack(i: int):
return i - 1
def sortArray(nums: list)->list:
leng = len(nums)
#
for i in range(int(leng / 2), 0, -1):
sink(nums, i, leng)
k = leng
#
while k > 1:
swap(nums, 1, k)
k -= 1
sink(nums, 1, k)
return nums
def swap(nums: list, i: int, j: int):
temp = nums[rollBack(i)]
nums[rollBack(i)] = nums[rollBack(j)]
nums[rollBack(j)] = temp
def sink(nums: list, k: int, end: int):
while 2 * k <= end:
j = 2 * k
if j + 1 <= end and nums[rollBack(j + 1)] > nums[rollBack(j)]:
j += 1
if nums[rollBack(j)] > nums[rollBack(k)]:
swap(nums, j, k)
else:
break
k = j
```
广
****
O(n O(nlogn), O(nlogn)
****
i _ 2 i _ 2+1 i _ 2 + 1 i _ 2 + 2 O(1),
****
1.访访 CPU
2.