为什么我的HeapSort跳过数组中的第一个元素?

时间:2013-03-17 23:39:47

标签: c++ heapsort

我在我的算法类中为一个赋值实现了HeapSort,我终于完成了,但由于某种原因,排序正在跳过我的数组中的第一个元素。

原始阵列:

int heapArray[SIZE] = {  5 ,99, 32, 4, 1, 12, 15 , 8, 13, 55 };

HeapSort()之后的输出

5 99 32 15 13 12 8 4 1

Iv经历了所有的功能,无法弄清楚为什么它会跳过第一个元素。谁能帮我吗? Iv包括下面的所有HeapSort功能。我知道它很多,但我很接近。

int main()
{
int heapArray[SIZE] = {  5 ,99, 32, 4, 1, 12, 15 , 8, 13, 55 };
HeapSort(heapArray);


return 0;

}

............................................... ..........................................

void HeapSort(int heapArray[])
{   
int heap_size = SIZE;
int n = SIZE;
int temp;

Build_Max_Heap(heapArray);

for(int i = n-1; i >= 2; i--)
{
    temp = heapArray[1];
    heapArray[1] = heapArray[i];
    heapArray[i] = temp;

    heap_size = heap_size-1;
    Max_Heapify(heapArray,1);
}

return;
}

............................................... ............................................

void Build_Max_Heap(int heapArray[])
{
double n = SIZE;

for(double i = floor((n-1)/2); i >= 1; i--)
{
    Max_Heapify(heapArray,i);
}

return;
}

............................................... ............................................

void Max_Heapify(int heapArray[],int i)
{
int n = SIZE;
int largest = 0;
int l = Left(i);
int r = Right(i);

if(( l <= n) && (heapArray[l] > heapArray[i]))
{
    largest = l;
}
else
{
    largest = i;
}

if( (r <= n) && ( heapArray[r] > heapArray[largest]))
{
    largest = r;
}

int temp;
if(largest != i)
{
    temp = heapArray[i];
    heapArray[i] = heapArray[largest];
    heapArray[largest] = temp;

    Max_Heapify(heapArray,largest);
}

return;
}

............................................... ............................................

int Parent(int i)
{
return (i/2);
}

int Left(int i)
{
return (2*i);
}

int Right(int i)
{
return ((2*i)+1);
}

3 个答案:

答案 0 :(得分:1)

您的代码中有几个问题:

首先:数组索引从0开始而不是1,因此,有许多索引错误的地方列出如下:

在HeapSort函数中:

for(int i = n-1; i >= 2; i--)  {
                   //should be 1 not 2
    temp = heapArray[1]; //should be 0 not 1 in those 2 statements
    heapArray[1] = heapArray[i];
     heapArray[i] = temp;  
}

Max_Heapify(heapArray,1);
                  //should start from 0 not 1

此外:

for(double i = floor((n-1)/2); i >= 1; i--)
{                             //should start from 0 not 1
    Max_Heapify(heapArray,i);
}

你父,左,右有类似的错误,你可以看到上面两个帖子。

同时,您在HeapSort和Max_Heapfiy函数中存在逻辑错误。 您定义heap_size并更新它,但您从未真正使用过它。您必须将其传递给Max_Heapify函数。因为每次将当前最大元素放到数组末尾时,您只需要将剩余元素堆积而不是整个堆。这也意味着你的heapify函数有错误,因为你从未真正使用过heap_size。正确的HeapSort和Max_Heapify以及Build_Max_Heap函数应为:

堆排序:

void HeapSort(int heapArray[]) 
{   
  //do what you did before this sentence
  Build_Max_Heap(heapArray,heap_size); 
  //need to pass heap_size since Max_Heapify use heap_size
  for(int i = n-1; i >= 1; i--)
  {
     //...same as you did but pay attention to index
     heap_size = heap_size-1;
     Max_Heapify(heapArray,0,heap_size); //remember to use heap_size
   }
   //you declared void, no need to return anything
 }

Build_Max_Heap函数:

void Build_Max_Heap(int heapArray[], int heapSize) //should use heapSize
{
    int n = SIZE;
    for(int i = floor((n-1)/2); i >= 0; i--) //here should be 0 not 1
   {
      Max_Heapify(heapArray,i,heapSize); //should use heapSize
    }
 }

Max_Heapify功能:

void Max_Heapify(int heapArray[],int i, int heap_size) //should use heap_size
{
    //....here similar to what you did
    if(( l < heap_size) && (heapArray[l] > heapArray[i]))
    {   //^^compare with heap_size not SIZE
        //skip identical part
    }

    if( (r < heaps_ize) && ( heapArray[r] > heapArray[largest]))
    {
       //same error as above
    }

   //skip identical part again
   Max_Heapify(heapArray,largest, heap_size); //have to use heap_size
 }
}

您可能需要从伪代码中更好地理解HeapSort算法。

答案 1 :(得分:0)

这里

Build_Max_Heap(heapArray);

for(int i = n-1; i >= 2; i--)
{                 ^^^^^
    temp = heapArray[1];
    heapArray[1] = heapArray[i];
    heapArray[i] = temp;

    heap_size = heap_size-1;
    Max_Heapify(heapArray,1);
}

return;
}

你停止在i = 2上循环,将它减少到1,不要采用由 0

索引的heapArray的第一个元素

答案 2 :(得分:0)

ParentLeftRight函数适合基于1的数组。对于从0开始,您需要使用:

int Parent(int i)
{
    return (i - 1) / 2;
}

int Left(int i)
{
    return (2 * i) + 1;
}

int Right(int i)
{
    return 2 * (i + 1);
}

您可以查看示例堆:

    0
   / \
  1   2
 / \ / \
3  4 5  6