SlideShare a Scribd company logo
1 of 7
Download to read offline
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

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

Sorting programs
Sorting programsSorting programs
Sorting programsVarun Garg
 
#include iostream using namespace std; class Array { priva.pdf
#include iostream using namespace std; class Array { priva.pdf#include iostream using namespace std; class Array { priva.pdf
#include iostream using namespace std; class Array { priva.pdfANSAPPARELS
 
CBSE Class XII Comp sc practical file
CBSE Class XII Comp sc practical fileCBSE Class XII Comp sc practical file
CBSE Class XII Comp sc practical filePranav Ghildiyal
 
Write a program that obtains the execution time of selection sort, bu.pdf
Write a program that obtains the execution time of selection sort, bu.pdfWrite a program that obtains the execution time of selection sort, bu.pdf
Write a program that obtains the execution time of selection sort, bu.pdfarri2009av
 
Write a program to implement and test the following sorting algorithm.docx
 Write a program to implement and test the following sorting algorithm.docx Write a program to implement and test the following sorting algorithm.docx
Write a program to implement and test the following sorting algorithm.docxajoy21
 
#include stdio.h #include stdlib.h #include time.hdouble.pdf
#include stdio.h #include stdlib.h #include time.hdouble.pdf#include stdio.h #include stdlib.h #include time.hdouble.pdf
#include stdio.h #include stdlib.h #include time.hdouble.pdfarjunchetri1
 
a) Write the recursive function in C++ to sort a set of data using M.pdf
a) Write the recursive function in C++ to sort a set of data using M.pdfa) Write the recursive function in C++ to sort a set of data using M.pdf
a) Write the recursive function in C++ to sort a set of data using M.pdfnageswara1958
 
DAA Lab File C Programs
DAA Lab File C ProgramsDAA Lab File C Programs
DAA Lab File C ProgramsKandarp Tiwari
 
Data structure output 1
Data structure output 1Data structure output 1
Data structure output 1Balaji Thala
 
I have written the code but cannot complete the assignment please help.pdf
I have written the code but cannot complete the assignment please help.pdfI have written the code but cannot complete the assignment please help.pdf
I have written the code but cannot complete the assignment please help.pdfshreeaadithyaacellso
 
Chapter 8 c solution
Chapter 8 c solutionChapter 8 c solution
Chapter 8 c solutionAzhar Javed
 

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

Sorting programs
Sorting programsSorting programs
Sorting programs
 
DataStructures notes
DataStructures notesDataStructures notes
DataStructures notes
 
Cpds lab
Cpds labCpds lab
Cpds lab
 
VTU Data Structures Lab Manual
VTU Data Structures Lab ManualVTU Data Structures Lab Manual
VTU Data Structures Lab Manual
 
#include iostream using namespace std; class Array { priva.pdf
#include iostream using namespace std; class Array { priva.pdf#include iostream using namespace std; class Array { priva.pdf
#include iostream using namespace std; class Array { priva.pdf
 
CBSE Class XII Comp sc practical file
CBSE Class XII Comp sc practical fileCBSE Class XII Comp sc practical file
CBSE Class XII Comp sc practical file
 
Write a program that obtains the execution time of selection sort, bu.pdf
Write a program that obtains the execution time of selection sort, bu.pdfWrite a program that obtains the execution time of selection sort, bu.pdf
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
 Write a program to implement and test the following sorting algorithm.docx Write a program to implement and test the following sorting algorithm.docx
Write a program to implement and test the following sorting algorithm.docx
 
oodp elab.pdf
oodp elab.pdfoodp elab.pdf
oodp elab.pdf
 
#include stdio.h #include stdlib.h #include time.hdouble.pdf
#include stdio.h #include stdlib.h #include time.hdouble.pdf#include stdio.h #include stdlib.h #include time.hdouble.pdf
#include stdio.h #include stdlib.h #include time.hdouble.pdf
 
Arrays
ArraysArrays
Arrays
 
a) Write the recursive function in C++ to sort a set of data using M.pdf
a) Write the recursive function in C++ to sort a set of data using M.pdfa) Write the recursive function in C++ to sort a set of data using M.pdf
a) Write the recursive function in C++ to sort a set of data using M.pdf
 
SlideSet_4_Arraysnew.pdf
SlideSet_4_Arraysnew.pdfSlideSet_4_Arraysnew.pdf
SlideSet_4_Arraysnew.pdf
 
DAA Lab File C Programs
DAA Lab File C ProgramsDAA Lab File C Programs
DAA Lab File C Programs
 
Data structure output 1
Data structure output 1Data structure output 1
Data structure output 1
 
I have written the code but cannot complete the assignment please help.pdf
I have written the code but cannot complete the assignment please help.pdfI have written the code but cannot complete the assignment please help.pdf
I have written the code but cannot complete the assignment please help.pdf
 
Chapter 8 c solution
Chapter 8 c solutionChapter 8 c solution
Chapter 8 c solution
 
Lab Question
Lab QuestionLab Question
Lab Question
 
week-21x
week-21xweek-21x
week-21x
 
Scala 2 + 2 > 4
Scala 2 + 2 > 4Scala 2 + 2 > 4
Scala 2 + 2 > 4
 

More from ANJALIENTERPRISES1

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

More from ANJALIENTERPRISES1 (20)

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

Recently uploaded

Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersChitralekhaTherkar
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 

Recently uploaded (20)

Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of Powders
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 

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(); }