当从数组更改为数组 unsigned int 时,堆排序 C 不起作用

Heap Sort C doesn't work when change from array to array unsigned int

当所有变量和数组都定义为 int 时,我的代码可以正常运行,但是如果我将类型更改为 unsigned int,它将无法运行。
这是我的代码:

#include <stdio.h>
#include <stdlib.h>
void swap(unsigned int *a, unsigned int *b){
unsigned int temp;
    temp=*a ; *a=*b ; *b=temp;
}
void heapify(unsigned int A[], unsigned int i, unsigned int n){ 
//sort from root i

    unsigned int L = 2*i+1; 
// node left child
    unsigned int R = 2*i+2; 
//node  right child
    unsigned int max = i; 
//set max node is root
    if(L < n && A[L] > A[max]) max = L; 
//if node child > max, set that node is max
    if(R < n && A[R] > A[max]) max = R; 
    if(max != i) {
        swap(&A[i], &A[max]);
        heapify(A, max, n);//recursive tree with root is node swaped
        }

}

void buildHeap(unsigned int A[], unsigned int n){

    unsigned int i = n/2 - 1;
    for(; i >= 0; i--) heapify(A, i, n);
    }

void heapSort(unsigned int A[], unsigned int n){

    buildHeap(A, n);
    unsigned int i = n-1;
    for(; i >= 0; i--){
        swap(&A[0], &A[i]);
        heapify(A, 0, i);
}
}

void PrintArray(unsigned int A[], unsigned int n){
    unsigned int i;
    for(i = 0; i < n; i++){
        printf("%d ", A[i]);
    }
}
int main(){
    unsigned int A[]={1,6,8,9,7,1,65,92,2,9,2,5,73,9,1,5};
    unsigned int n=sizeof (A)/sizeof(unsigned int);
    PrintArray(A,n);
    heapSort(A,n);
    PrintArray(A,n);
}

输出卡在打印旧数组时,之后没有任何反应。

我该怎么办?

问题出在 buildHeap - 您将迭代器设置为 unsigned int,并且 运行 循环直到 i 不再为正 - 这是现金unsigned int 永远不会发生。

void buildHeap(unsigned int A[], unsigned int n){

    unsigned int i = n/2 - 1;
    for(; i >= 0; i--) heapify(A, i, n);
    }