检测到堆腐蚀:正常阻塞(#138)后的0x005205​​68

时间:2017-03-12 19:20:51

标签: c++ algorithm heapsort heap-corruption

#include<iostream>
#include <stdio.h>   
#include <stdlib.h>     
#include <time.h>

using namespace std;

void max_heapify(int *a, int i, int n)
{
    int j, temp;
    temp = a[i];
    j = 2*i;
    while (j <= n)
    {
        if (j < n && a[j+1] > a[j])
        {
            j = j+1;
        }
        if (temp > a[j])
        {
            break;
        }
        else if (temp <= a[j])
        {
            a[j/2] = a[j];
            j = 2*j;
        }
    }
    a[j/2] = temp;
    return;
}
void heapsort(int *a, int n)
{
    int i, temp;
    for (i = n; i >= 2; i--)
    {
        temp = a[i];
        a[i] = a[1];
        a[1] = temp;
        max_heapify(a, 1, i - 1);
    }
}
void build_maxheap(int *a, int n)
{
    int i;
    for(i = n/2; i >= 1; i--)
    {
        max_heapify(a, i, n);
    }
}

int main()
{
    clock_t t,t2;
    int * a = new int[10]; 
    int any = 9;
    for(int i = 0; i < 10; i++)
    {
        a[i] = rand() % 9 + 1;  
    }
    t = clock();
    build_maxheap(a,10);
    heapsort(a, 10);
    t2 = clock();

    cout<<endl<<endl<<t2-t<<endl<<endl;

    delete [] a;

    system("pause");

}

我正在尝试在随机排序的数组上运行heapsort以检查它需要多长时间。但它给了我堆腐败的错误。我从网上拿走算法。我在合并和插入排序上运行了相同的数组,它工作正常。

0 个答案:

没有答案
相关问题