Update 堆排序.md

去掉堆排序多申请的数组
This commit is contained in:
xiaox0321 2022-02-18 19:05:04 +08:00 committed by GitHub
parent 54b468e155
commit 65b616c987
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -219,40 +219,39 @@ def sink(nums: list, index: int, len: int):
Java Code: Java Code:
```java ```java
class Solution { public class Solution {
int rollBack(int i) {
return i - 1;
}
public int[] sortArray(int[] nums) { public int[] sortArray(int[] nums) {
int len = nums.length; 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) { for (int i = len / 2; i >= 1; --i) {
sink(a,i,len); sink(nums, i, len);
} }
int k = len; int k = len;
//排序 //排序
while (k > 1) { while (k > 1) {
swap(a,1,k--); swap(nums, 1, k--);
sink(a,1,k); sink(nums, 1, k);
}
for (int i = 1; i < len+1; ++i) {
nums[i-1] = a[i];
} }
return nums; return nums;
} }
public void sink(int[] nums, int k, int end) { public void sink(int[] nums, int k, int end) {
//下沉 //下沉
while (2 * k <= end) { while (2 * k <= end) {
int j = 2 * k; 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++; j++;
} }
if (nums[j] > nums[k]) { if (nums[rollBack(j)] > nums[rollBack(k)]) {
swap(nums, j, k); swap(nums, j, k);
} else { } else {
break; break;
@ -260,10 +259,11 @@ class Solution {
k = j; k = j;
} }
} }
public void swap (int nums[], int i, int j) {
int temp = nums[i]; void swap(int[] nums, int i, int j) {
nums[i] = nums[j]; int temp = nums[rollBack(i)];
nums[j] = temp; nums[rollBack(i)] = nums[rollBack(j)];
nums[rollBack(j)] = temp;
} }
} }
@ -272,39 +272,40 @@ class Solution {
Python Code: Python Code:
```python ```python
def rollBack(i: int):
return i - 1
def sortArray(nums: list)->list: def sortArray(nums: list)->list:
leng = len(nums) leng = len(nums)
a = [0] + nums
# 下沉建堆 # 下沉建堆
for i in range(int(leng / 2), 0, -1): for i in range(int(leng / 2), 0, -1):
sink(a, i, leng) sink(nums, i, leng)
k = leng k = leng
# 排序 # 排序
while k > 1: while k > 1:
swap(a, 1, k) swap(nums, 1, k)
k -= 1 k -= 1
sink(a, 1, k) sink(nums, 1, k)
for i in range(1, leng + 1):
nums[i - 1] = a[i]
return nums return nums
def swap(nums: list, i: int, j: int): def swap(nums: list, i: int, j: int):
temp = nums[i] temp = nums[rollBack(i)]
nums[i] = nums[j] nums[rollBack(i)] = nums[rollBack(j)]
nums[j] = temp nums[rollBack(j)] = temp
def sink(nums: list, k: int, end: int): def sink(nums: list, k: int, end: int):
while 2 * k <= end: while 2 * k <= end:
j = 2 * k 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 j += 1
if nums[j] > nums[k]: if nums[rollBack(j)] > nums[rollBack(k)]:
swap(nums, j, k) swap(nums, j, k)
else: else:
break break
k = j k = j
``` ```
好啦堆排序我们就到这里啦是不是搞定啦总的来说堆排序比其他排序算法稍微难理解一些重点就是建堆而且应用比较广泛大家记得打卡呀 好啦堆排序我们就到这里啦是不是搞定啦总的来说堆排序比其他排序算法稍微难理解一些重点就是建堆而且应用比较广泛大家记得打卡呀