Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

初始堆时起始指针问题 #38

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions 7.heapSort.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ var len; // 因为声明的多个函数都需要数据长度,所以把len

function buildMaxHeap(arr) { // 建立大顶堆
len = arr.length;
for (var i = Math.floor(len/2); i >= 0; i--) {
//从最后一个非叶子结点开始
for (var i = Math.floor(len/2) -1; i >= 0; i--) {
heapify(arr, i);
}
}
Expand Down Expand Up @@ -77,7 +78,7 @@ function heapSort(arr) {
```python
def buildMaxHeap(arr):
import math
for i in range(math.floor(len(arr)/2),-1,-1):
for i in range(math.floor(len(arr)/2-1),-1,-1):
heapify(arr,i)

def heapify(arr, i):
Expand Down Expand Up @@ -122,7 +123,7 @@ func heapSort(arr []int) []int {
}

func buildMaxHeap(arr []int, arrLen int) {
for i := arrLen / 2; i >= 0; i-- {
for i := arrLen / 2 - 1; i >= 0; i-- {
heapify(arr, i, arrLen)
}
}
Expand Down Expand Up @@ -171,7 +172,7 @@ public class HeapSort implements IArraySort {
}

private void buildMaxHeap(int[] arr, int len) {
for (int i = (int) Math.floor(len / 2); i >= 0; i--) {
for (int i = len / 2 - 1; i >= 0; i--) {
heapify(arr, i, len);
}
}
Expand Down Expand Up @@ -210,7 +211,7 @@ public class HeapSort implements IArraySort {
function buildMaxHeap(&$arr)
{
global $len;
for ($i = floor($len/2); $i >= 0; $i--) {
for ($i = floor($len/2) - 1; $i >= 0; $i--) {
heapify($arr, $i);
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/java/main/HeapSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ public int[] sort(int[] sourceArray) throws Exception {
}

private void buildMaxHeap(int[] arr, int len) {
for (int i = (int) Math.floor(len / 2); i >= 0; i--) {
//从最后一个非叶子结点开始
for (int i = len / 2 - 1; i >= 0; i--) {
heapify(arr, i, len);
}
}
Expand Down