Search code examples
c++heapheapsort

C++ Heapsort raise error


This is my full implementation code with some notes. I tried gathering some pieces of code from here and there

#include <iostream>
#include <math.h> 
#include <conio.h>
#include "Header.h"
using namespace std;
void swap(int &x, int &y)
{
int temp = x;
x = y;
y = temp;
}
heap::heap()
{
/*
int x[10] = { 1, 12, 33, 44, 2, 6, 7, 8, 9, 10 };
heapsize = 10;
SetData(x);
*/
}
bool heap::empty()
{
return heapsize == 0;
}
int heap::parent(int index)
{
int x;
if (index % 2 == 0)
{
    x = ((index - 1) / 2);
}
else
{
    x = (index / 2);
}
return x;
}
int heap::left(int index)
    {
int a;
a = 2 * index ;
    return a;
    }
    int heap::right(int index)
    {
int a;
a = 2 * index + 1 ;
return a;
}

void heap::Heapify(int i)
{
int temp, l, r, largest;
l = left(i);
r = right(i);
if ((l <= heapsize) && (A[l] > A[i]))
    largest = l;
else largest = i;
if ((r <= heapsize) && (A[r] > A[largest]))
    largest = r;
if (i != largest)
{
    //swap(A[i], A[largest]);
    temp = A[i];
    A[i] = A[largest];
    A[largest] = temp;
    Heapify(largest);
}
}
void heap::Buildheap()
{
for (int i = heapsize ; i > 0; i--)
    Heapify(i);
}

void heap::insert()
{
int insert;
heapsize = (heapsize + 1);
cout << "insert please:" << endl;
cin >> insert;
int temp = insert;
Buildheap();
cout << "Done!" << endl;
}
void heap::viewheap()
{
Buildheap();

for (int count = 0; count <= heapsize ; count++)
{
    cout << A[count]<<" ";
}
cout << endl << endl;
}
void heap::heapsort()
{
Buildheap();
int temp;
for (int i = 0; i < heapsize; i++)
{
    //temp = A[heapsize -1];
    //A[heapsize -1] = A[0];
    //swap(A[1], A[i]);
    swap(A[1], A[i]);
    heapsize--; 
    Heapify(1);
    /*
    temp = A[heapsize - 1];
    A[heapsize - 1] = A[0];
    heapsize--;
    Buildheap();
    */
}
viewheap();
}
int heap::getmax(int)
{
int temp;
if (empty())
    cout << "Queue is EMPTY!" << endl;
else
{
    Buildheap();
    temp = A[0];
}
return temp;
}
void heap::rmax()
{
if (empty())
    cout << "Queue is EMPTY!" << endl;
else
{
    Buildheap();
    A[0] = A[heapsize];
    heapsize--;

    Heapify(0);
}
}
void heap::SetData(int x[])
{
for (int i = 0; i <= (heapsize); i++)
{
    A[i] = x[i];
}
}

and this is my Header :

class heap
{
private:
int parent(int);
int left(int);
int right(int);
void Buildheap();

void Heapify(int);
public:
int A[100];
heap();
void insert();
bool empty();
void heapsort();
int getmax(int);
void rmax();
int heapsize;
void viewheap();
void SetData(int[]);
};

My code raise error.


Solution

  • In implementation of parent instead of

     int heap::parent(int index){
        int x;
        if (index % 2 == 0){
          x = ((index - 1) / 2);
        } else {
          x = (index / 2);
        }
        return x;
     }
    

    you can simply write

      int heap::parent(int index){
        return index / 2;
     }
    

    in the method insert, you read new int from console and then call method Build without passing anything, so method Build won't know anything about int 'insert'. When you want to insert new element, you iterate through the whole array and call Heapify for each index. So in your algorithm inserting new element requires nlogn time complexity, which is very huge, inserting an element in heap may be done in logn time. You just need to review algorithm for inserting in heap. Here is some demo which describes inserting instructions http://upload.wikimedia.org/wikipedia/commons/4/4d/Heapsort-example.gif

    also heapsort algorithm

    http://en.wikipedia.org/wiki/Heapsort