Hello:
Here is the source code you requested, I used C++ Builder 6, but you
must have no problems to use the non-visual C++ code in another
compiler. You can download the complete project including forms and
the precompiled exe from this url:
http://www.xpde.com/Heap.zip
And below is the source code:
Unit1.h
//---------------------------------------------------------------------------
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
///---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TButton *btnSort;
TListBox *lbItems;
TButton *btnAdd;
TButton *btnClear;
TButton *btnExit;
void __fastcall btnSortClick(TObject *Sender);
void __fastcall btnClearClick(TObject *Sender);
void __fastcall btnAddClick(TObject *Sender);
void __fastcall btnExitClick(TObject *Sender);
private: // User declarations
int a[255]; //255 elements max
int heapsize;
int elements;
public: // User declarations
__fastcall TForm1(TComponent* Owner);
void heapsort();
void buildheap();
void heapify(int i);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
Unit1.cpp
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include "Unit2.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void TForm1::heapsort()
{
int t;
int k;
buildheap();
for (k=elements;k>=1;k--)
{
t=a[k];
a[k]=a[0];
a[0]=t;
heapsize--;
heapify(0);
}
}
void TForm1::buildheap()
{
int j;
heapsize=elements+1;
for (j=((elements+1) / 2) - 1;j>=0;j--) heapify(j);
}
void TForm1::heapify(int i)
{
int l;
int r;
int max;
int t;
l=2*i+1;
r=2*i+2;
if ((l<heapsize) && (a[l]>a[i])) max=l;
else max=i;
if ((r<heapsize) && (a[r]>a[max])) max=r;
if (max!=i)
{
t=a[max];
a[max]=a[i];
a[i]=t;
heapify(max);
}
}
void __fastcall TForm1::btnSortClick(TObject *Sender)
{
int k;
elements=lbItems->Items->Count-1;
for (k=0;k<=elements;k++) a[k]=lbItems->Items->operator
[](k).ToInt();
heapsort();
Form2->lbSorted->Items->Clear();
for (k=0;k<=elements;k++)
{
Form2->lbSorted->Items->Add(AnsiString(a[k]));
}
Form2->ShowModal();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btnClearClick(TObject *Sender)
{
lbItems->Items->Clear();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btnAddClick(TObject *Sender)
{
AnsiString value;
InputQuery("Add value","Value",value);
lbItems->Items->Add(value);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btnExitClick(TObject *Sender)
{
Close();
}
//---------------------------------------------------------------------------
The another form is just to hold the sorted results, no code involved,
download the zip mentioned earlier to get the full working project. I
hope this is what you were looking for and don't hesitate to request
for any clarification.
No url references for this answer because is just an implementation of
a well-known algorithm.
Regards. |