Google Answers Logo
View Question
 
Q: Heap sort code of C++ ( Answered 5 out of 5 stars,   0 Comments )
Question  
Subject: Heap sort code of C++
Category: Computers > Programming
Asked by: aprilchiang-ga
List Price: $15.00
Posted: 30 Nov 2002 21:15 PST
Expires: 30 Dec 2002 21:15 PST
Question ID: 117077
i need entire code of using "heap sort" to sort several integers.Must
using visual interface.(Form1: enter values in the box, once a
integer. then press "sort" botton; Form 2:show the sorted values, and
let user to choose repeat the program or exit)

Request for Question Clarification by joseleon-ga on 01 Dec 2002 07:39 PST
Hello, aprilchiang:
  Because you want visual interface, for which compiler and OS do you
want this code? C++ Builder is ok for you?

Regards.

Clarification of Question by aprilchiang-ga on 01 Dec 2002 09:13 PST
i just want it could run on windows 98 or xp. i usually write the code
with Broland C++ builder. it will be great if you use C++..
thanks a lot
Answer  
Subject: Re: Heap sort code of C++
Answered By: joseleon-ga on 02 Dec 2002 07:27 PST
Rated:5 out of 5 stars
 
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.

Request for Answer Clarification by aprilchiang-ga on 02 Dec 2002 17:49 PST
hello jpseleon,

thanks a lot for your help. i have copied the code into my bourland
C++ 5 and recomplier it. it shows :
"[C++ Error] Unit1.cpp[76]:E2316 'operator[]' is not a member of
'TStrings'" with the line:
a[k]=lbItems->Items->operator[](k).ToInt();
i don't know how to fix it.. is it because the different edition of
the compiler?
thanks for your help again...

Clarification of Answer by joseleon-ga on 03 Dec 2002 05:53 PST
Hello:
 Sorry, I used C++Builder 6 with the new extensions, you can just
change that line to this:

    for (k=0;k<=elements;k++) a[k]=lbItems->Items->Strings[k].ToInt();

It's better you download the full code, from the previous url instead
of copying and pasting, because you will need the Form resources.

Any other problem? don't hesitate to ask ;-)

Regards.
aprilchiang-ga rated this answer:5 out of 5 stars and gave an additional tip of: $5.00
thanks a lot

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy