heapsort-code无效

时间:2015-08-29 22:22:11

标签: 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])
        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;
}

1 个答案:

答案 0 :(得分: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;
}

for (int q=(strlength)/2;q==1;--q)错了。你的意思是for (int q=(strlength)/2;q>1;--q)。您的循环仅在q==1时运行。

a=new int[length-1];数组的大小应为length,而不是length-1。即使swap(a[1],a[length]);错误,因为a[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;
}