SlideShare a Scribd company logo
i am using C++ coding
source coding:
// Program that sorts an array using Heap Sort method
# include
# include
const int MAX = 10 ;
class array
{
private :
int arr[MAX] ;
int count ;
public :
array( ) ;
void add ( int num ) ;
void makeheap( ) ;
void heapsort( ) ;
void display( ) ;
} ;
// initialises data member
array :: array( )
{
count = 0 ;
for ( int i = 0 ; i < MAX ; i++ )
arr[MAX] = 0 ;
}
// adds element to an array
void array :: add ( int num )
{
if ( count < MAX )
{
arr[count] = num ;
count++ ;
}
else
cout << " Array is full" << endl ;
}
// creates heap from the tree
void array :: makeheap( )
{
for ( int i = 1 ; i < count ; i++ )
{
int val = arr[i] ;
int s = i ;
int f = ( s - 1 ) / 2 ;
while ( s > 0 && arr[f] < val )
{
arr[s] = arr[f] ;
s = f ;
f = ( s - 1 ) / 2 ;
}
arr[s] = val ;
}
}
// sorts heap
void array :: heapsort( )
{
for ( int i = count - 1 ; i > 0 ; i-- )
{
int ivalue = arr[i] ;
arr[i] = arr[0] ;
int f = 0 ;
int s ;
if ( i == 1 )
s = -1 ;
else
s = 1 ;
if ( i > 2 && arr[2] > arr[1] )
s = 2 ;
while ( s >= 0 && ivalue < arr[s] )
{
arr[f] = arr[s] ;
f = s ;
s = 2 * f + 1 ;
if ( s + 1 <= i - 1 && arr[s] < arr[s + 1] )
s++ ;
if ( s > i - 1 )
s = -1 ;
}
arr[f] = ivalue ;
}
}
// displays contents
void array :: display( )
{
for ( int i = 0 ; i < count ; i++ )
cout << arr[i] << "t" ;
cout << endl ;
}
void main( )
{
array a ;
clrscr();
a.add ( 11 ) ;
a.add ( 2 ) ;
a.add ( 9 ) ;
a.add ( 13 ) ;
a.add ( 57 ) ;
a.add ( 25 ) ;
a.add ( 17 ) ;
a.add ( 1 ) ;
a.add ( 90 ) ;
a.add ( 3 ) ;
a.makeheap( ) ;
cout << " Heap Sort. " ;
cout << " Before Sorting: " ;
a.display( ) ;
a.heapsort( ) ;
cout << " After Sorting: " ;
a.display( ) ;
getch();
}
Solution
i am using C++ coding
source coding:
// Program that sorts an array using Heap Sort method
# include
# include
const int MAX = 10 ;
class array
{
private :
int arr[MAX] ;
int count ;
public :
array( ) ;
void add ( int num ) ;
void makeheap( ) ;
void heapsort( ) ;
void display( ) ;
} ;
// initialises data member
array :: array( )
{
count = 0 ;
for ( int i = 0 ; i < MAX ; i++ )
arr[MAX] = 0 ;
}
// adds element to an array
void array :: add ( int num )
{
if ( count < MAX )
{
arr[count] = num ;
count++ ;
}
else
cout << " Array is full" << endl ;
}
// creates heap from the tree
void array :: makeheap( )
{
for ( int i = 1 ; i < count ; i++ )
{
int val = arr[i] ;
int s = i ;
int f = ( s - 1 ) / 2 ;
while ( s > 0 && arr[f] < val )
{
arr[s] = arr[f] ;
s = f ;
f = ( s - 1 ) / 2 ;
}
arr[s] = val ;
}
}
// sorts heap
void array :: heapsort( )
{
for ( int i = count - 1 ; i > 0 ; i-- )
{
int ivalue = arr[i] ;
arr[i] = arr[0] ;
int f = 0 ;
int s ;
if ( i == 1 )
s = -1 ;
else
s = 1 ;
if ( i > 2 && arr[2] > arr[1] )
s = 2 ;
while ( s >= 0 && ivalue < arr[s] )
{
arr[f] = arr[s] ;
f = s ;
s = 2 * f + 1 ;
if ( s + 1 <= i - 1 && arr[s] < arr[s + 1] )
s++ ;
if ( s > i - 1 )
s = -1 ;
}
arr[f] = ivalue ;
}
}
// displays contents
void array :: display( )
{
for ( int i = 0 ; i < count ; i++ )
cout << arr[i] << "t" ;
cout << endl ;
}
void main( )
{
array a ;
clrscr();
a.add ( 11 ) ;
a.add ( 2 ) ;
a.add ( 9 ) ;
a.add ( 13 ) ;
a.add ( 57 ) ;
a.add ( 25 ) ;
a.add ( 17 ) ;
a.add ( 1 ) ;
a.add ( 90 ) ;
a.add ( 3 ) ;
a.makeheap( ) ;
cout << " Heap Sort. " ;
cout << " Before Sorting: " ;
a.display( ) ;
a.heapsort( ) ;
cout << " After Sorting: " ;
a.display( ) ;
getch();
}

More Related Content

PDF
check the modifed code now you will get all operations done.termin.pdf
DOCX
Solutionsfor co2 C Programs for data structures
PDF
#include stdio.h#include stdlib.h#include string.h#inclu.pdf
DOC
Basic c programs updated on 31.8.2020
PDF
Can you give an example of a binary heap programCan you give an .pdf
DOCX
Data Structures Using C Practical File
PDF
Program of sorting using shell sort #include stdio.h #de.pdf
DOC
Daapracticals 111105084852-phpapp02
check the modifed code now you will get all operations done.termin.pdf
Solutionsfor co2 C Programs for data structures
#include stdio.h#include stdlib.h#include string.h#inclu.pdf
Basic c programs updated on 31.8.2020
Can you give an example of a binary heap programCan you give an .pdf
Data Structures Using C Practical File
Program of sorting using shell sort #include stdio.h #de.pdf
Daapracticals 111105084852-phpapp02

Similar to i am using C++ codingsource coding Program that sorts an arra.pdf (20)

DOC
Sorting programs
DOCX
DataStructures notes
DOCX
PDF
VTU Data Structures Lab Manual
PDF
codes.txt.pdf code presentation engineering
PDF
#include iostream using namespace std; class Array { priva.pdf
DOC
CBSE Class XII Comp sc practical file
PDF
Write a program that obtains the execution time of selection sort, bu.pdf
DOCX
Write a program to implement and test the following sorting algorithm.docx
DOCX
Data structure lab on practical computer science.docx
DOCX
Data Structure lab on a practical in computer science
PDF
oodp elab.pdf
PDF
#include stdio.h #include stdlib.h #include time.hdouble.pdf
DOCX
PDF
a) Write the recursive function in C++ to sort a set of data using M.pdf
PDF
SlideSet_4_Arraysnew.pdf
DOCX
DAA Lab File C Programs
DOCX
Data structure output 1
PDF
I have written the code but cannot complete the assignment please help.pdf
DOCX
Chapter 8 c solution
Sorting programs
DataStructures notes
VTU Data Structures Lab Manual
codes.txt.pdf code presentation engineering
#include iostream using namespace std; class Array { priva.pdf
CBSE Class XII Comp sc practical file
Write a program that obtains the execution time of selection sort, bu.pdf
Write a program to implement and test the following sorting algorithm.docx
Data structure lab on practical computer science.docx
Data Structure lab on a practical in computer science
oodp elab.pdf
#include stdio.h #include stdlib.h #include time.hdouble.pdf
a) Write the recursive function in C++ to sort a set of data using M.pdf
SlideSet_4_Arraysnew.pdf
DAA Lab File C Programs
Data structure output 1
I have written the code but cannot complete the assignment please help.pdf
Chapter 8 c solution

More from ANJALIENTERPRISES1 (20)

PDF
H2SO4 may act as 1) an acidexample 2NaOH + H2SO4 - Na2SO4 +.pdf
PDF
Computer graphics are pictures and movies produced use computers fre.pdf
PDF
Circle.javaimport java.text.DecimalFormat;public class Circle {.pdf
PDF
C is correct. Only the host that the unicast message is addressed to.pdf
PDF
According to the seriousness level, the following organs areThymu.pdf
PDF
Among 40 subjects randomly choose 20 subjects and assign themSol.pdf
PDF
AdvantagesThe main objective of business combination is to elimina.pdf
PDF
a. Germ cell speciication in Drosophila is primarily a maternlly con.pdf
PDF
A is correct. The packets to be filtered would be heading into the r.pdf
PDF
1). A) ipsilateralBelow the point of spinal cord, the nerve paths .pdf
PDF
-FSolution-F.pdf
PDF
17-The Y-Chromosome DNA testing helps in the examination of the male.pdf
PDF
#includestdio.h#includestring.h#includestdlib.h#define M.pdf
PDF
The pH and pOH of a solution are defined as pH .pdf
PDF
goal_state = [1, 8, 7, 2, 0, 6, 3, 4, 5] #goal_state = [1, 0, 7, 2, .pdf
PDF
7 steps are required as 9 is the 7th element and the search is line.pdf
PDF
(D) the number of moles of hydroxide ion added and the number of mol.pdf
PDF
Look for changes in oxidation numbers. These occ.pdf
PDF
D) Insulin .pdf
PDF
Use Daltons Law of partial pressures. P(Total).pdf
H2SO4 may act as 1) an acidexample 2NaOH + H2SO4 - Na2SO4 +.pdf
Computer graphics are pictures and movies produced use computers fre.pdf
Circle.javaimport java.text.DecimalFormat;public class Circle {.pdf
C is correct. Only the host that the unicast message is addressed to.pdf
According to the seriousness level, the following organs areThymu.pdf
Among 40 subjects randomly choose 20 subjects and assign themSol.pdf
AdvantagesThe main objective of business combination is to elimina.pdf
a. Germ cell speciication in Drosophila is primarily a maternlly con.pdf
A is correct. The packets to be filtered would be heading into the r.pdf
1). A) ipsilateralBelow the point of spinal cord, the nerve paths .pdf
-FSolution-F.pdf
17-The Y-Chromosome DNA testing helps in the examination of the male.pdf
#includestdio.h#includestring.h#includestdlib.h#define M.pdf
The pH and pOH of a solution are defined as pH .pdf
goal_state = [1, 8, 7, 2, 0, 6, 3, 4, 5] #goal_state = [1, 0, 7, 2, .pdf
7 steps are required as 9 is the 7th element and the search is line.pdf
(D) the number of moles of hydroxide ion added and the number of mol.pdf
Look for changes in oxidation numbers. These occ.pdf
D) Insulin .pdf
Use Daltons Law of partial pressures. P(Total).pdf

Recently uploaded (20)

PDF
01-Introduction-to-Information-Management.pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PDF
RMMM.pdf make it easy to upload and study
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Complications of Minimal Access Surgery at WLH
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Classroom Observation Tools for Teachers
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
01-Introduction-to-Information-Management.pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
RMMM.pdf make it easy to upload and study
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Complications of Minimal Access Surgery at WLH
Microbial disease of the cardiovascular and lymphatic systems
Supply Chain Operations Speaking Notes -ICLT Program
202450812 BayCHI UCSC-SV 20250812 v17.pptx
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Classroom Observation Tools for Teachers
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
O5-L3 Freight Transport Ops (International) V1.pdf

i am using C++ codingsource coding Program that sorts an arra.pdf

  • 1. i am using C++ coding source coding: // Program that sorts an array using Heap Sort method # include # include const int MAX = 10 ; class array { private : int arr[MAX] ; int count ; public : array( ) ; void add ( int num ) ; void makeheap( ) ; void heapsort( ) ; void display( ) ; } ; // initialises data member array :: array( ) { count = 0 ; for ( int i = 0 ; i < MAX ; i++ ) arr[MAX] = 0 ; } // adds element to an array void array :: add ( int num ) { if ( count < MAX ) { arr[count] = num ; count++ ; } else cout << " Array is full" << endl ;
  • 2. } // creates heap from the tree void array :: makeheap( ) { for ( int i = 1 ; i < count ; i++ ) { int val = arr[i] ; int s = i ; int f = ( s - 1 ) / 2 ; while ( s > 0 && arr[f] < val ) { arr[s] = arr[f] ; s = f ; f = ( s - 1 ) / 2 ; } arr[s] = val ; } } // sorts heap void array :: heapsort( ) { for ( int i = count - 1 ; i > 0 ; i-- ) { int ivalue = arr[i] ; arr[i] = arr[0] ; int f = 0 ; int s ; if ( i == 1 ) s = -1 ; else s = 1 ; if ( i > 2 && arr[2] > arr[1] ) s = 2 ; while ( s >= 0 && ivalue < arr[s] ) { arr[f] = arr[s] ;
  • 3. f = s ; s = 2 * f + 1 ; if ( s + 1 <= i - 1 && arr[s] < arr[s + 1] ) s++ ; if ( s > i - 1 ) s = -1 ; } arr[f] = ivalue ; } } // displays contents void array :: display( ) { for ( int i = 0 ; i < count ; i++ ) cout << arr[i] << "t" ; cout << endl ; } void main( ) { array a ; clrscr(); a.add ( 11 ) ; a.add ( 2 ) ; a.add ( 9 ) ; a.add ( 13 ) ; a.add ( 57 ) ; a.add ( 25 ) ; a.add ( 17 ) ; a.add ( 1 ) ; a.add ( 90 ) ; a.add ( 3 ) ; a.makeheap( ) ; cout << " Heap Sort. " ; cout << " Before Sorting: " ; a.display( ) ; a.heapsort( ) ;
  • 4. cout << " After Sorting: " ; a.display( ) ; getch(); } Solution i am using C++ coding source coding: // Program that sorts an array using Heap Sort method # include # include const int MAX = 10 ; class array { private : int arr[MAX] ; int count ; public : array( ) ; void add ( int num ) ; void makeheap( ) ; void heapsort( ) ; void display( ) ; } ; // initialises data member array :: array( ) { count = 0 ; for ( int i = 0 ; i < MAX ; i++ ) arr[MAX] = 0 ; } // adds element to an array void array :: add ( int num ) { if ( count < MAX )
  • 5. { arr[count] = num ; count++ ; } else cout << " Array is full" << endl ; } // creates heap from the tree void array :: makeheap( ) { for ( int i = 1 ; i < count ; i++ ) { int val = arr[i] ; int s = i ; int f = ( s - 1 ) / 2 ; while ( s > 0 && arr[f] < val ) { arr[s] = arr[f] ; s = f ; f = ( s - 1 ) / 2 ; } arr[s] = val ; } } // sorts heap void array :: heapsort( ) { for ( int i = count - 1 ; i > 0 ; i-- ) { int ivalue = arr[i] ; arr[i] = arr[0] ; int f = 0 ; int s ; if ( i == 1 ) s = -1 ; else
  • 6. s = 1 ; if ( i > 2 && arr[2] > arr[1] ) s = 2 ; while ( s >= 0 && ivalue < arr[s] ) { arr[f] = arr[s] ; f = s ; s = 2 * f + 1 ; if ( s + 1 <= i - 1 && arr[s] < arr[s + 1] ) s++ ; if ( s > i - 1 ) s = -1 ; } arr[f] = ivalue ; } } // displays contents void array :: display( ) { for ( int i = 0 ; i < count ; i++ ) cout << arr[i] << "t" ; cout << endl ; } void main( ) { array a ; clrscr(); a.add ( 11 ) ; a.add ( 2 ) ; a.add ( 9 ) ; a.add ( 13 ) ; a.add ( 57 ) ; a.add ( 25 ) ; a.add ( 17 ) ; a.add ( 1 ) ; a.add ( 90 ) ;
  • 7. a.add ( 3 ) ; a.makeheap( ) ; cout << " Heap Sort. " ; cout << " Before Sorting: " ; a.display( ) ; a.heapsort( ) ; cout << " After Sorting: " ; a.display( ) ; getch(); }