C++ heapsort代码不工作

C++ heapsort代码不工作,c++,heapsort,C++,Heapsort,以下代码不适用于堆排序。我觉得还可以。有人能帮我吗?我遵循了CLRS中的伪代码,在遍历算法后,排序的数字不会被更新 #include <iostream> using namespace std; void max_heapify(int *b, int i,int he_size) { int l,r,largest; l=2*i; r=(2*i+1); if (l<=he_size && b[l]>b[i])

以下代码不适用于堆排序。我觉得还可以。有人能帮我吗?我遵循了CLRS中的伪代码,在遍历算法后,排序的数字不会被更新

#include <iostream>
using namespace std;

void max_heapify(int *b, int i,int he_size)
{
    int l,r,largest;
    l=2*i;
    r=(2*i+1);
    if (l<=he_size && b[l]>b[i])
        largest=l;
    else largest=i;
    if (r<=he_size && b[r]> b[largest])
        largest=r;

    if (largest!=i)
    {
        swap(b[i],b[largest]);
        max_heapify(b,largest,he_size);
    }

}

void build_max_heap(int *c,int h_size,int strlength)
{
    for (int q=(strlength)/2;q==1;--q)
    {
        max_heapify(c,q,h_size);
    }
}

void swap(int a, int b)
{
    int c=b;
    b=a;
    a=c;
}

int main()
{
    int length;
    int heap_size;
    cout<<"Enter the number of numbers to be sorted by heap sort"<<endl;
    cin>>length;

    int* a=NULL;
    a=new int[length-1];
    int temp;

    cout<<"Enter the numbers"<<endl;
    for(int i=0;i<length;i++)
    {
        cin>>temp;
        *(a+i)=temp;
    }

    cout<<"The given numbers are:"<<endl;
    for(int j=0;j<length;j++)
        cout<<*(a+j)<<" "<<endl;
    heap_size= length;
    build_max_heap(a,heap_size,length);
    for (int l=length;l==2;--l)
    {
        swap(a[1],a[length]);
        heap_size=heap_size-1;
        max_heapify(a,1,heap_size);
    }
    cout<<"The sorted numbers are:"<<endl;
    for(int j=0;j<length;j++)
        cout<<*(a+j)<<" "<<endl;

    system("pause");
    return 0;
}

代码中的错误数量是巨大的。很抱歉这么说

void swap(int a, int b)
{
    int c=b;
    b=a;
    a=c;
}
不执行任何操作-a和b应通过链接传递,而不是通过值传递:

void swap(int &a, int &b)
{
    int c=b;
    b=a;
    a=c;
}
对于int q=strlength/2;q==1-q是错误的。你的意思是int q=strlength/2;q> 一,-Q您的循环仅在q==1时运行

a=新整数[长度-1];数组的大小应为length,而不是length-1。即使swapa[1],a[length];是错误的,因为[length]超出了数组

算法上也有一些错误。我试图重写尽可能少的代码

正确的代码是:

#include <iostream>

using namespace std;

void sift_down(int *a, int start, int end) {
    int root = start;

    while (root * 2 + 1 <= end) {
        int child = root * 2 + 1;
        int sw    = root;

        if (a[sw] < a[child])
            sw = child;

        if (child + 1 <= end and a[sw] < a[child + 1])
            sw = child + 1;
        if (sw == root)
            return;
        else
            swap(a[root], a[sw]);
        root = sw;
    }
}

void max_heapify(int *b, int count) {
    int start = (count - 2) / 2;

    while (start >= 0) {
        sift_down(b, start, count - 1);
        --start;
    }
}

void swap(int &a, int &b) {
    int c = b;
    b = a;
    a = c;
}

int main() {
    int length;
    int heap_size;
    cout << "Enter the number of numbers to be sorted by heap sort" << endl;
    cin >> length;

    int *a = new int[length];

    cout << "Enter the numbers" << endl;
    for (int i = 0; i < length; i++) {
        cin >> a[i];
    }

    cout << "The given numbers are:" << endl;
    for (int j = 0; j < length; j++)
        cout << a[j] << " ";
    cout << endl;

    heap_size = length;
    max_heapify(a, heap_size);

    --heap_size;
    while (heap_size) {
        swap(a[heap_size], a[0]);
        --heap_size;
        sift_down(a, 0, heap_size);
    }
    cout << "The sorted numbers are:" << endl;
    for (int j = 0; j < length; j++)
        cout << a[j] << " ";
    cout << endl;

    //system("pause");
    return 0;
}

交换函数不执行任何操作,因为它按值获取其参数。它需要引用它们,或者只使用std::swap。@JonathanPotter谢谢你,你能提供关于如何使用std::swap的更多细节吗?我是一个初学者,我没有使用向量…我在:使用std::swap中使用了std::swapas;swapb[i],b[最大];但错误依然存在,你试图一次学到太多新东西。分别使用std::swap、std::vector和按引用传递,并在尝试一起使用它们之前掌握它们。具有讽刺意味的是,所发布的代码,如果它在您的实现中编译,那么max_heapify很可能使用std::swap。您的本地掉期没有原型和掉期[i],b[最大];出现在文件中唯一正式声明不正确交换之前。如果这样编译,您在代码中提供的交换在main之前是未使用的,在main之前,它是所选择的交换,并且正如所指出的那样,完全断开。