mirror of
https://github.com/chefyuan/algorithm-base.git
synced 2024-12-25 11:58:53 +00:00
Update 堆排序.md
去掉堆排序多申请的数组
This commit is contained in:
parent
54b468e155
commit
65b616c987
@ -219,40 +219,39 @@ def sink(nums: list, index: int, len: int):
|
||||
Java Code:
|
||||
|
||||
```java
|
||||
class Solution {
|
||||
public class Solution {
|
||||
|
||||
int rollBack(int i) {
|
||||
return i - 1;
|
||||
}
|
||||
|
||||
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);
|
||||
for (int i = len / 2; i >= 1; --i) {
|
||||
sink(nums, 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];
|
||||
swap(nums, 1, k--);
|
||||
sink(nums, 1, k);
|
||||
}
|
||||
|
||||
return nums;
|
||||
}
|
||||
public void sink (int[] nums, int k,int end) {
|
||||
|
||||
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]) {
|
||||
if (j + 1 <= end && nums[rollBack(j + 1)] > nums[rollBack(j)]) {
|
||||
j++;
|
||||
}
|
||||
if (nums[j] > nums[k]) {
|
||||
if (nums[rollBack(j)] > nums[rollBack(k)]) {
|
||||
swap(nums, j, k);
|
||||
} else {
|
||||
break;
|
||||
@ -260,10 +259,11 @@ class Solution {
|
||||
k = j;
|
||||
}
|
||||
}
|
||||
public void swap (int nums[], int i, int j) {
|
||||
int temp = nums[i];
|
||||
nums[i] = nums[j];
|
||||
nums[j] = temp;
|
||||
|
||||
void swap(int[] nums, int i, int j) {
|
||||
int temp = nums[rollBack(i)];
|
||||
nums[rollBack(i)] = nums[rollBack(j)];
|
||||
nums[rollBack(j)] = temp;
|
||||
}
|
||||
|
||||
}
|
||||
@ -272,39 +272,40 @@ class Solution {
|
||||
Python Code:
|
||||
|
||||
```python
|
||||
def rollBack(i: int):
|
||||
return i - 1
|
||||
|
||||
def sortArray(nums: list)->list:
|
||||
leng = len(nums)
|
||||
a = [0] + nums
|
||||
# 下沉建堆
|
||||
for i in range(int(leng / 2), 0, -1):
|
||||
sink(a, i, leng)
|
||||
sink(nums, i, leng)
|
||||
|
||||
k = leng
|
||||
# 排序
|
||||
while k > 1:
|
||||
swap(a, 1, k)
|
||||
swap(nums, 1, k)
|
||||
k -= 1
|
||||
sink(a, 1, k)
|
||||
sink(nums, 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
|
||||
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[j + 1] > nums[j]:
|
||||
if j + 1 <= end and nums[rollBack(j + 1)] > nums[rollBack(j)]:
|
||||
j += 1
|
||||
if nums[j] > nums[k]:
|
||||
if nums[rollBack(j)] > nums[rollBack(k)]:
|
||||
swap(nums, j, k)
|
||||
else:
|
||||
break
|
||||
k = j
|
||||
|
||||
```
|
||||
|
||||
好啦,堆排序我们就到这里啦,是不是搞定啦,总的来说堆排序比其他排序算法稍微难理解一些,重点就是建堆,而且应用比较广泛,大家记得打卡呀。
|
||||
|
Loading…
Reference in New Issue
Block a user