SlideShare a Scribd company logo
1 of 12
Download to read offline
a) Write the recursive function in C++ to sort a set of data using Merge Sort
b) Given a set of data as below:
5 10 15 29 34 42 79 87
Ans:)
merge sort:
merge sort is a one of the sorting technicis a sorting technique which divides the array into 2 sub
arrays which have size 2 and merge adjacent pair. so that after first process you have n/2 array of
size 2. this process is repeated until only one array remaining of size n.
a)
ans)
Source code: C++ program
// Program that sorts an array using merge sort
# include
# include
const int MAX = 4 ;
class array
{
private :
int *arr ;
int size ;
int count ;
public :
array( ) ;
array ( int sz ) ;
void add ( int num ) ;
void display( ) ;
static void sort ( int *a, int sz ) ;
void merge ( array &a, array &b ) ;
~array( ) ;
} ;
// initializes data member
array :: array( )
{
count = size = 0 ;
arr = NULL ;
}
// initialises data member
array :: array( int sz )
{
count = 0 ;
size = sz ;
arr = new int[sz] ;
}
// adds a new element to the array
void array :: add ( int num )
{
if ( count < size )
{
arr[count] = num ;
count++ ;
}
else
cout << " Array is full" << endl ;
}
// displays elements in an array
void array :: display( )
{
for ( int i = 0 ; i < count ; i++ )
cout << arr[i] << "t" ;
cout << endl ;
}
// merges two arrays
void array :: merge ( array &a, array &b )
{
sort ( a.arr, a.size ) ;
sort ( b.arr, b.size ) ;
size = a.count + b.count ;
arr = new int[size] ;
int i, j, k ;
for ( i = j = k = 0 ; j < a.count || k < b.count ; )
{
if ( a.arr[j] <= b.arr[k] )
arr[i++] = a.arr[j++] ;
else
arr[i++] = b.arr[k++] ;
count++ ;
if ( j == a.count || k == b.count )
break ;
}
for ( ; j < a.count ; )
{
arr[i++] = a.arr[j++] ;
count++ ;
}
for ( ; k < b.count ; )
{
arr[i++] = b.arr[k++] ;
count++ ;
}
}
// sorts an array
void array :: sort ( int *a, int sz )
{
int temp ;
for ( int i = 0 ; i <= sz - 2 ; i++ )
{
for ( int j = i + 1 ; j <= sz - 1 ; j++ )
{
if ( a[i] > a[j] )
{
temp = a[i] ;
a[i] = a[j] ;
a[j] = temp ;
}
}
}
}
// deallocates memory
array :: ~array( )
{
delete arr ;
}
void main( )
{
array a ( MAX ) ;
clrscr();
a.add ( 29 ) ;
a.add ( 5) ;
a.add ( 10) ;
a.add ( 15 ) ;
cout << " Merge sort. " ;
cout << " First array: " << endl ;
a.display( ) ;
array b ( MAX ) ;
b.add ( 34 ) ;
b.add ( 87 ) ;
b.add ( 79 ) ;
b.add ( 42) ;
cout << " Second array: " << endl ;
b.display( ) ;
array c ;
c.merge ( a, b ) ;
cout << " Array after sorting: " << endl ;
c.display( ) ;
getch();
}
i)insert
// initializes data member
array :: array( )
{
count = size = 0 ;
arr = NULL ;
}
// initialises data member
array :: array( int sz )
{
count = 0 ;
size = sz ;
arr = new int[sz] ;
}
// adds a new element to the array
void array :: add ( int num )
{
if ( count < size )
{
arr[count] = num ;
count++ ;
}
else
cout << " Array is full" << endl ;
}
the above code is used to inset of data in array.
ii. Selection
// displays elements in an array
void array :: display( )
{
for ( int i = 0 ; i < count ; i++ )
cout << arr[i] << "t" ;
cout << endl ;
}
iii. Merge
merge two arrays
// merges two arrays
void array :: merge ( array &a, array &b )
{
sort ( a.arr, a.size ) ;
sort ( b.arr, b.size ) ;
size = a.count + b.count ;
arr = new int[size] ;
int i, j, k ;
for ( i = j = k = 0 ; j < a.count || k < b.count ; )
{
if ( a.arr[j] <= b.arr[k] )
arr[i++] = a.arr[j++] ;
else
arr[i++] = b.arr[k++] ;
count++ ;
if ( j == a.count || k == b.count )
break ;
}
for ( ; j < a.count ; )
{
arr[i++] = a.arr[j++] ;
count++ ;
}
for ( ; k < b.count ; )
{
arr[i++] = b.arr[k++] ;
count++ ;
}
}
Solution
a) Write the recursive function in C++ to sort a set of data using Merge Sort
b) Given a set of data as below:
5 10 15 29 34 42 79 87
Ans:)
merge sort:
merge sort is a one of the sorting technicis a sorting technique which divides the array into 2 sub
arrays which have size 2 and merge adjacent pair. so that after first process you have n/2 array of
size 2. this process is repeated until only one array remaining of size n.
a)
ans)
Source code: C++ program
// Program that sorts an array using merge sort
# include
# include
const int MAX = 4 ;
class array
{
private :
int *arr ;
int size ;
int count ;
public :
array( ) ;
array ( int sz ) ;
void add ( int num ) ;
void display( ) ;
static void sort ( int *a, int sz ) ;
void merge ( array &a, array &b ) ;
~array( ) ;
} ;
// initializes data member
array :: array( )
{
count = size = 0 ;
arr = NULL ;
}
// initialises data member
array :: array( int sz )
{
count = 0 ;
size = sz ;
arr = new int[sz] ;
}
// adds a new element to the array
void array :: add ( int num )
{
if ( count < size )
{
arr[count] = num ;
count++ ;
}
else
cout << " Array is full" << endl ;
}
// displays elements in an array
void array :: display( )
{
for ( int i = 0 ; i < count ; i++ )
cout << arr[i] << "t" ;
cout << endl ;
}
// merges two arrays
void array :: merge ( array &a, array &b )
{
sort ( a.arr, a.size ) ;
sort ( b.arr, b.size ) ;
size = a.count + b.count ;
arr = new int[size] ;
int i, j, k ;
for ( i = j = k = 0 ; j < a.count || k < b.count ; )
{
if ( a.arr[j] <= b.arr[k] )
arr[i++] = a.arr[j++] ;
else
arr[i++] = b.arr[k++] ;
count++ ;
if ( j == a.count || k == b.count )
break ;
}
for ( ; j < a.count ; )
{
arr[i++] = a.arr[j++] ;
count++ ;
}
for ( ; k < b.count ; )
{
arr[i++] = b.arr[k++] ;
count++ ;
}
}
// sorts an array
void array :: sort ( int *a, int sz )
{
int temp ;
for ( int i = 0 ; i <= sz - 2 ; i++ )
{
for ( int j = i + 1 ; j <= sz - 1 ; j++ )
{
if ( a[i] > a[j] )
{
temp = a[i] ;
a[i] = a[j] ;
a[j] = temp ;
}
}
}
}
// deallocates memory
array :: ~array( )
{
delete arr ;
}
void main( )
{
array a ( MAX ) ;
clrscr();
a.add ( 29 ) ;
a.add ( 5) ;
a.add ( 10) ;
a.add ( 15 ) ;
cout << " Merge sort. " ;
cout << " First array: " << endl ;
a.display( ) ;
array b ( MAX ) ;
b.add ( 34 ) ;
b.add ( 87 ) ;
b.add ( 79 ) ;
b.add ( 42) ;
cout << " Second array: " << endl ;
b.display( ) ;
array c ;
c.merge ( a, b ) ;
cout << " Array after sorting: " << endl ;
c.display( ) ;
getch();
}
i)insert
// initializes data member
array :: array( )
{
count = size = 0 ;
arr = NULL ;
}
// initialises data member
array :: array( int sz )
{
count = 0 ;
size = sz ;
arr = new int[sz] ;
}
// adds a new element to the array
void array :: add ( int num )
{
if ( count < size )
{
arr[count] = num ;
count++ ;
}
else
cout << " Array is full" << endl ;
}
the above code is used to inset of data in array.
ii. Selection
// displays elements in an array
void array :: display( )
{
for ( int i = 0 ; i < count ; i++ )
cout << arr[i] << "t" ;
cout << endl ;
}
iii. Merge
merge two arrays
// merges two arrays
void array :: merge ( array &a, array &b )
{
sort ( a.arr, a.size ) ;
sort ( b.arr, b.size ) ;
size = a.count + b.count ;
arr = new int[size] ;
int i, j, k ;
for ( i = j = k = 0 ; j < a.count || k < b.count ; )
{
if ( a.arr[j] <= b.arr[k] )
arr[i++] = a.arr[j++] ;
else
arr[i++] = b.arr[k++] ;
count++ ;
if ( j == a.count || k == b.count )
break ;
}
for ( ; j < a.count ; )
{
arr[i++] = a.arr[j++] ;
count++ ;
}
for ( ; k < b.count ; )
{
arr[i++] = b.arr[k++] ;
count++ ;
}
}

More Related Content

Similar to a) Write the recursive function in C++ to sort a set of data using M.pdf

C (PPS)Programming for problem solving.pptx
C (PPS)Programming for problem solving.pptxC (PPS)Programming for problem solving.pptx
C (PPS)Programming for problem solving.pptxrohinitalekar1
 
Array 31.8.2020 updated
Array 31.8.2020 updatedArray 31.8.2020 updated
Array 31.8.2020 updatedvrgokila
 
Arrays_in_c++.pptx
Arrays_in_c++.pptxArrays_in_c++.pptx
Arrays_in_c++.pptxMrMaster11
 
Question In C Programming In mathematics, a set is a colle...Sav.pdf
Question In C Programming In mathematics, a set is a colle...Sav.pdfQuestion In C Programming In mathematics, a set is a colle...Sav.pdf
Question In C Programming In mathematics, a set is a colle...Sav.pdfarihantcomp1008
 
Queue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked ListQueue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked ListPTCL
 
Write a function called Reverse that takes in two parameters- an integ.docx
Write a function called Reverse that takes in two parameters- an integ.docxWrite a function called Reverse that takes in two parameters- an integ.docx
Write a function called Reverse that takes in two parameters- an integ.docxlez31palka
 
Synapse india dotnet development overloading operater part 3
Synapse india dotnet development overloading operater part 3Synapse india dotnet development overloading operater part 3
Synapse india dotnet development overloading operater part 3Synapseindiappsdevelopment
 
Solve the coding errors for upvotemake test-statsg++ -g -std=c++.pdf
Solve the coding errors for upvotemake test-statsg++ -g -std=c++.pdfSolve the coding errors for upvotemake test-statsg++ -g -std=c++.pdf
Solve the coding errors for upvotemake test-statsg++ -g -std=c++.pdfsnewfashion
 
Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functionsSwarup Boro
 
#include String.hpp#include ..Functionsfunctions.hpp.docx
#include String.hpp#include ..Functionsfunctions.hpp.docx#include String.hpp#include ..Functionsfunctions.hpp.docx
#include String.hpp#include ..Functionsfunctions.hpp.docxgertrudebellgrove
 
Data structure array
Data structure  arrayData structure  array
Data structure arrayMajidHamidAli
 
CountPositiveNumbersInArray.javapackage org.students;import java.pdf
CountPositiveNumbersInArray.javapackage org.students;import java.pdfCountPositiveNumbersInArray.javapackage org.students;import java.pdf
CountPositiveNumbersInArray.javapackage org.students;import java.pdfaparnatiwari291
 

Similar to a) Write the recursive function in C++ to sort a set of data using M.pdf (20)

C (PPS)Programming for problem solving.pptx
C (PPS)Programming for problem solving.pptxC (PPS)Programming for problem solving.pptx
C (PPS)Programming for problem solving.pptx
 
Array 31.8.2020 updated
Array 31.8.2020 updatedArray 31.8.2020 updated
Array 31.8.2020 updated
 
Arrays_in_c++.pptx
Arrays_in_c++.pptxArrays_in_c++.pptx
Arrays_in_c++.pptx
 
Question In C Programming In mathematics, a set is a colle...Sav.pdf
Question In C Programming In mathematics, a set is a colle...Sav.pdfQuestion In C Programming In mathematics, a set is a colle...Sav.pdf
Question In C Programming In mathematics, a set is a colle...Sav.pdf
 
Chapter2
Chapter2Chapter2
Chapter2
 
Chap 6 c++
Chap 6 c++Chap 6 c++
Chap 6 c++
 
Chap 6 c++
Chap 6 c++Chap 6 c++
Chap 6 c++
 
Queue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked ListQueue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked List
 
Write a function called Reverse that takes in two parameters- an integ.docx
Write a function called Reverse that takes in two parameters- an integ.docxWrite a function called Reverse that takes in two parameters- an integ.docx
Write a function called Reverse that takes in two parameters- an integ.docx
 
Synapse india dotnet development overloading operater part 3
Synapse india dotnet development overloading operater part 3Synapse india dotnet development overloading operater part 3
Synapse india dotnet development overloading operater part 3
 
Solve the coding errors for upvotemake test-statsg++ -g -std=c++.pdf
Solve the coding errors for upvotemake test-statsg++ -g -std=c++.pdfSolve the coding errors for upvotemake test-statsg++ -g -std=c++.pdf
Solve the coding errors for upvotemake test-statsg++ -g -std=c++.pdf
 
6_Array.pptx
6_Array.pptx6_Array.pptx
6_Array.pptx
 
Pointers and arrays
Pointers and arraysPointers and arrays
Pointers and arrays
 
Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functions
 
#include String.hpp#include ..Functionsfunctions.hpp.docx
#include String.hpp#include ..Functionsfunctions.hpp.docx#include String.hpp#include ..Functionsfunctions.hpp.docx
#include String.hpp#include ..Functionsfunctions.hpp.docx
 
Arrays
ArraysArrays
Arrays
 
Data structure array
Data structure  arrayData structure  array
Data structure array
 
Arrays and strings in c++
Arrays and strings in c++Arrays and strings in c++
Arrays and strings in c++
 
CountPositiveNumbersInArray.javapackage org.students;import java.pdf
CountPositiveNumbersInArray.javapackage org.students;import java.pdfCountPositiveNumbersInArray.javapackage org.students;import java.pdf
CountPositiveNumbersInArray.javapackage org.students;import java.pdf
 
SlideSet_4_Arraysnew.pdf
SlideSet_4_Arraysnew.pdfSlideSet_4_Arraysnew.pdf
SlideSet_4_Arraysnew.pdf
 

More from nageswara1958

0.0.176Solution0.0.176.pdf
0.0.176Solution0.0.176.pdf0.0.176Solution0.0.176.pdf
0.0.176Solution0.0.176.pdfnageswara1958
 
#include iostream #include fstream #include stack #inclu.pdf
#include iostream #include fstream #include stack #inclu.pdf#include iostream #include fstream #include stack #inclu.pdf
#include iostream #include fstream #include stack #inclu.pdfnageswara1958
 
Initial investment = $2,600,000+80,000+120,000+100,000= $2,900,00.pdf
 Initial investment = $2,600,000+80,000+120,000+100,000= $2,900,00.pdf Initial investment = $2,600,000+80,000+120,000+100,000= $2,900,00.pdf
Initial investment = $2,600,000+80,000+120,000+100,000= $2,900,00.pdfnageswara1958
 
the concentration increases .pdf
                     the concentration increases                      .pdf                     the concentration increases                      .pdf
the concentration increases .pdfnageswara1958
 
It is an anhydride its struture like maleic anh.pdf
                     It is an anhydride its struture like maleic anh.pdf                     It is an anhydride its struture like maleic anh.pdf
It is an anhydride its struture like maleic anh.pdfnageswara1958
 
In most of the invertebrates, these structures ar.pdf
                     In most of the invertebrates, these structures ar.pdf                     In most of the invertebrates, these structures ar.pdf
In most of the invertebrates, these structures ar.pdfnageswara1958
 
difficult to understand. I tried but your questio.pdf
                     difficult to understand. I tried but your questio.pdf                     difficult to understand. I tried but your questio.pdf
difficult to understand. I tried but your questio.pdfnageswara1958
 
Cl- has the larger ionization energy since it onl.pdf
                     Cl- has the larger ionization energy since it onl.pdf                     Cl- has the larger ionization energy since it onl.pdf
Cl- has the larger ionization energy since it onl.pdfnageswara1958
 
Strategic planning is a method for composing a plan which relates th.pdf
Strategic planning is a method for composing a plan which relates th.pdfStrategic planning is a method for composing a plan which relates th.pdf
Strategic planning is a method for composing a plan which relates th.pdfnageswara1958
 
HAsO4- as it can accept an electron to form HAsO4.pdf
                     HAsO4- as it can accept an electron to form HAsO4.pdf                     HAsO4- as it can accept an electron to form HAsO4.pdf
HAsO4- as it can accept an electron to form HAsO4.pdfnageswara1958
 
Solution.cpp #include iostreamheader file for input output f.pdf
Solution.cpp #include iostreamheader file for input output f.pdfSolution.cpp #include iostreamheader file for input output f.pdf
Solution.cpp #include iostreamheader file for input output f.pdfnageswara1958
 
pH of the solution = 4.5                -log [H+] = 4.5         .pdf
pH of the solution = 4.5                -log [H+] = 4.5         .pdfpH of the solution = 4.5                -log [H+] = 4.5         .pdf
pH of the solution = 4.5                -log [H+] = 4.5         .pdfnageswara1958
 
Essentially, treatment with LDATHF will produce .pdf
                     Essentially, treatment with LDATHF will produce .pdf                     Essentially, treatment with LDATHF will produce .pdf
Essentially, treatment with LDATHF will produce .pdfnageswara1958
 
no real roots existsSolutionno real roots exists.pdf
no real roots existsSolutionno real roots exists.pdfno real roots existsSolutionno real roots exists.pdf
no real roots existsSolutionno real roots exists.pdfnageswara1958
 
Kp for the new reaction = 1ZSolutionKp for the new reaction =.pdf
Kp for the new reaction = 1ZSolutionKp for the new reaction =.pdfKp for the new reaction = 1ZSolutionKp for the new reaction =.pdf
Kp for the new reaction = 1ZSolutionKp for the new reaction =.pdfnageswara1958
 
In the given source code the Person class do not implement the Compa.pdf
In the given source code the Person class do not implement the Compa.pdfIn the given source code the Person class do not implement the Compa.pdf
In the given source code the Person class do not implement the Compa.pdfnageswara1958
 
Hello!Lets look at the balanced reaction equation starting with th.pdf
Hello!Lets look at the balanced reaction equation starting with th.pdfHello!Lets look at the balanced reaction equation starting with th.pdf
Hello!Lets look at the balanced reaction equation starting with th.pdfnageswara1958
 
HereVoume of NaOH left Unreacted = 7000.10 - 0.20300= 10 mlH.pdf
HereVoume of NaOH left Unreacted = 7000.10 - 0.20300= 10 mlH.pdfHereVoume of NaOH left Unreacted = 7000.10 - 0.20300= 10 mlH.pdf
HereVoume of NaOH left Unreacted = 7000.10 - 0.20300= 10 mlH.pdfnageswara1958
 
B H AS H oxidied himselb to H+ AND REDUCING HNO3.pdf
                     B H  AS H oxidied himselb to H+ AND REDUCING HNO3.pdf                     B H  AS H oxidied himselb to H+ AND REDUCING HNO3.pdf
B H AS H oxidied himselb to H+ AND REDUCING HNO3.pdfnageswara1958
 

More from nageswara1958 (20)

0.0.176Solution0.0.176.pdf
0.0.176Solution0.0.176.pdf0.0.176Solution0.0.176.pdf
0.0.176Solution0.0.176.pdf
 
#include iostream #include fstream #include stack #inclu.pdf
#include iostream #include fstream #include stack #inclu.pdf#include iostream #include fstream #include stack #inclu.pdf
#include iostream #include fstream #include stack #inclu.pdf
 
Initial investment = $2,600,000+80,000+120,000+100,000= $2,900,00.pdf
 Initial investment = $2,600,000+80,000+120,000+100,000= $2,900,00.pdf Initial investment = $2,600,000+80,000+120,000+100,000= $2,900,00.pdf
Initial investment = $2,600,000+80,000+120,000+100,000= $2,900,00.pdf
 
the concentration increases .pdf
                     the concentration increases                      .pdf                     the concentration increases                      .pdf
the concentration increases .pdf
 
It is an anhydride its struture like maleic anh.pdf
                     It is an anhydride its struture like maleic anh.pdf                     It is an anhydride its struture like maleic anh.pdf
It is an anhydride its struture like maleic anh.pdf
 
In most of the invertebrates, these structures ar.pdf
                     In most of the invertebrates, these structures ar.pdf                     In most of the invertebrates, these structures ar.pdf
In most of the invertebrates, these structures ar.pdf
 
difficult to understand. I tried but your questio.pdf
                     difficult to understand. I tried but your questio.pdf                     difficult to understand. I tried but your questio.pdf
difficult to understand. I tried but your questio.pdf
 
Cl- has the larger ionization energy since it onl.pdf
                     Cl- has the larger ionization energy since it onl.pdf                     Cl- has the larger ionization energy since it onl.pdf
Cl- has the larger ionization energy since it onl.pdf
 
Strategic planning is a method for composing a plan which relates th.pdf
Strategic planning is a method for composing a plan which relates th.pdfStrategic planning is a method for composing a plan which relates th.pdf
Strategic planning is a method for composing a plan which relates th.pdf
 
HAsO4- as it can accept an electron to form HAsO4.pdf
                     HAsO4- as it can accept an electron to form HAsO4.pdf                     HAsO4- as it can accept an electron to form HAsO4.pdf
HAsO4- as it can accept an electron to form HAsO4.pdf
 
Solution.cpp #include iostreamheader file for input output f.pdf
Solution.cpp #include iostreamheader file for input output f.pdfSolution.cpp #include iostreamheader file for input output f.pdf
Solution.cpp #include iostreamheader file for input output f.pdf
 
pH of the solution = 4.5                -log [H+] = 4.5         .pdf
pH of the solution = 4.5                -log [H+] = 4.5         .pdfpH of the solution = 4.5                -log [H+] = 4.5         .pdf
pH of the solution = 4.5                -log [H+] = 4.5         .pdf
 
Essentially, treatment with LDATHF will produce .pdf
                     Essentially, treatment with LDATHF will produce .pdf                     Essentially, treatment with LDATHF will produce .pdf
Essentially, treatment with LDATHF will produce .pdf
 
pSolutionp.pdf
pSolutionp.pdfpSolutionp.pdf
pSolutionp.pdf
 
no real roots existsSolutionno real roots exists.pdf
no real roots existsSolutionno real roots exists.pdfno real roots existsSolutionno real roots exists.pdf
no real roots existsSolutionno real roots exists.pdf
 
Kp for the new reaction = 1ZSolutionKp for the new reaction =.pdf
Kp for the new reaction = 1ZSolutionKp for the new reaction =.pdfKp for the new reaction = 1ZSolutionKp for the new reaction =.pdf
Kp for the new reaction = 1ZSolutionKp for the new reaction =.pdf
 
In the given source code the Person class do not implement the Compa.pdf
In the given source code the Person class do not implement the Compa.pdfIn the given source code the Person class do not implement the Compa.pdf
In the given source code the Person class do not implement the Compa.pdf
 
Hello!Lets look at the balanced reaction equation starting with th.pdf
Hello!Lets look at the balanced reaction equation starting with th.pdfHello!Lets look at the balanced reaction equation starting with th.pdf
Hello!Lets look at the balanced reaction equation starting with th.pdf
 
HereVoume of NaOH left Unreacted = 7000.10 - 0.20300= 10 mlH.pdf
HereVoume of NaOH left Unreacted = 7000.10 - 0.20300= 10 mlH.pdfHereVoume of NaOH left Unreacted = 7000.10 - 0.20300= 10 mlH.pdf
HereVoume of NaOH left Unreacted = 7000.10 - 0.20300= 10 mlH.pdf
 
B H AS H oxidied himselb to H+ AND REDUCING HNO3.pdf
                     B H  AS H oxidied himselb to H+ AND REDUCING HNO3.pdf                     B H  AS H oxidied himselb to H+ AND REDUCING HNO3.pdf
B H AS H oxidied himselb to H+ AND REDUCING HNO3.pdf
 

Recently uploaded

Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingTeacherCyreneCayanan
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docxPoojaSen20
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Shubhangi Sonawane
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...KokoStevan
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 

Recently uploaded (20)

Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 

a) Write the recursive function in C++ to sort a set of data using M.pdf

  • 1. a) Write the recursive function in C++ to sort a set of data using Merge Sort b) Given a set of data as below: 5 10 15 29 34 42 79 87 Ans:) merge sort: merge sort is a one of the sorting technicis a sorting technique which divides the array into 2 sub arrays which have size 2 and merge adjacent pair. so that after first process you have n/2 array of size 2. this process is repeated until only one array remaining of size n. a) ans) Source code: C++ program // Program that sorts an array using merge sort # include # include const int MAX = 4 ; class array { private : int *arr ; int size ; int count ; public : array( ) ; array ( int sz ) ; void add ( int num ) ; void display( ) ; static void sort ( int *a, int sz ) ; void merge ( array &a, array &b ) ; ~array( ) ; } ; // initializes data member array :: array( ) { count = size = 0 ; arr = NULL ;
  • 2. } // initialises data member array :: array( int sz ) { count = 0 ; size = sz ; arr = new int[sz] ; } // adds a new element to the array void array :: add ( int num ) { if ( count < size ) { arr[count] = num ; count++ ; } else cout << " Array is full" << endl ; } // displays elements in an array void array :: display( ) { for ( int i = 0 ; i < count ; i++ ) cout << arr[i] << "t" ; cout << endl ; } // merges two arrays void array :: merge ( array &a, array &b ) { sort ( a.arr, a.size ) ; sort ( b.arr, b.size ) ; size = a.count + b.count ; arr = new int[size] ; int i, j, k ; for ( i = j = k = 0 ; j < a.count || k < b.count ; ) {
  • 3. if ( a.arr[j] <= b.arr[k] ) arr[i++] = a.arr[j++] ; else arr[i++] = b.arr[k++] ; count++ ; if ( j == a.count || k == b.count ) break ; } for ( ; j < a.count ; ) { arr[i++] = a.arr[j++] ; count++ ; } for ( ; k < b.count ; ) { arr[i++] = b.arr[k++] ; count++ ; } } // sorts an array void array :: sort ( int *a, int sz ) { int temp ; for ( int i = 0 ; i <= sz - 2 ; i++ ) { for ( int j = i + 1 ; j <= sz - 1 ; j++ ) { if ( a[i] > a[j] ) { temp = a[i] ; a[i] = a[j] ; a[j] = temp ; } } } }
  • 4. // deallocates memory array :: ~array( ) { delete arr ; } void main( ) { array a ( MAX ) ; clrscr(); a.add ( 29 ) ; a.add ( 5) ; a.add ( 10) ; a.add ( 15 ) ; cout << " Merge sort. " ; cout << " First array: " << endl ; a.display( ) ; array b ( MAX ) ; b.add ( 34 ) ; b.add ( 87 ) ; b.add ( 79 ) ; b.add ( 42) ; cout << " Second array: " << endl ; b.display( ) ; array c ; c.merge ( a, b ) ; cout << " Array after sorting: " << endl ; c.display( ) ; getch(); } i)insert // initializes data member array :: array( ) {
  • 5. count = size = 0 ; arr = NULL ; } // initialises data member array :: array( int sz ) { count = 0 ; size = sz ; arr = new int[sz] ; } // adds a new element to the array void array :: add ( int num ) { if ( count < size ) { arr[count] = num ; count++ ; } else cout << " Array is full" << endl ; } the above code is used to inset of data in array. ii. Selection // displays elements in an array void array :: display( ) { for ( int i = 0 ; i < count ; i++ ) cout << arr[i] << "t" ; cout << endl ; } iii. Merge merge two arrays // merges two arrays void array :: merge ( array &a, array &b ) { sort ( a.arr, a.size ) ;
  • 6. sort ( b.arr, b.size ) ; size = a.count + b.count ; arr = new int[size] ; int i, j, k ; for ( i = j = k = 0 ; j < a.count || k < b.count ; ) { if ( a.arr[j] <= b.arr[k] ) arr[i++] = a.arr[j++] ; else arr[i++] = b.arr[k++] ; count++ ; if ( j == a.count || k == b.count ) break ; } for ( ; j < a.count ; ) { arr[i++] = a.arr[j++] ; count++ ; } for ( ; k < b.count ; ) { arr[i++] = b.arr[k++] ; count++ ; } } Solution a) Write the recursive function in C++ to sort a set of data using Merge Sort b) Given a set of data as below: 5 10 15 29 34 42 79 87 Ans:) merge sort: merge sort is a one of the sorting technicis a sorting technique which divides the array into 2 sub arrays which have size 2 and merge adjacent pair. so that after first process you have n/2 array of size 2. this process is repeated until only one array remaining of size n.
  • 7. a) ans) Source code: C++ program // Program that sorts an array using merge sort # include # include const int MAX = 4 ; class array { private : int *arr ; int size ; int count ; public : array( ) ; array ( int sz ) ; void add ( int num ) ; void display( ) ; static void sort ( int *a, int sz ) ; void merge ( array &a, array &b ) ; ~array( ) ; } ; // initializes data member array :: array( ) { count = size = 0 ; arr = NULL ; } // initialises data member array :: array( int sz ) { count = 0 ; size = sz ; arr = new int[sz] ; } // adds a new element to the array
  • 8. void array :: add ( int num ) { if ( count < size ) { arr[count] = num ; count++ ; } else cout << " Array is full" << endl ; } // displays elements in an array void array :: display( ) { for ( int i = 0 ; i < count ; i++ ) cout << arr[i] << "t" ; cout << endl ; } // merges two arrays void array :: merge ( array &a, array &b ) { sort ( a.arr, a.size ) ; sort ( b.arr, b.size ) ; size = a.count + b.count ; arr = new int[size] ; int i, j, k ; for ( i = j = k = 0 ; j < a.count || k < b.count ; ) { if ( a.arr[j] <= b.arr[k] ) arr[i++] = a.arr[j++] ; else arr[i++] = b.arr[k++] ; count++ ; if ( j == a.count || k == b.count ) break ; } for ( ; j < a.count ; )
  • 9. { arr[i++] = a.arr[j++] ; count++ ; } for ( ; k < b.count ; ) { arr[i++] = b.arr[k++] ; count++ ; } } // sorts an array void array :: sort ( int *a, int sz ) { int temp ; for ( int i = 0 ; i <= sz - 2 ; i++ ) { for ( int j = i + 1 ; j <= sz - 1 ; j++ ) { if ( a[i] > a[j] ) { temp = a[i] ; a[i] = a[j] ; a[j] = temp ; } } } } // deallocates memory array :: ~array( ) { delete arr ; } void main( ) { array a ( MAX ) ; clrscr();
  • 10. a.add ( 29 ) ; a.add ( 5) ; a.add ( 10) ; a.add ( 15 ) ; cout << " Merge sort. " ; cout << " First array: " << endl ; a.display( ) ; array b ( MAX ) ; b.add ( 34 ) ; b.add ( 87 ) ; b.add ( 79 ) ; b.add ( 42) ; cout << " Second array: " << endl ; b.display( ) ; array c ; c.merge ( a, b ) ; cout << " Array after sorting: " << endl ; c.display( ) ; getch(); } i)insert // initializes data member array :: array( ) { count = size = 0 ; arr = NULL ; } // initialises data member array :: array( int sz ) { count = 0 ; size = sz ; arr = new int[sz] ;
  • 11. } // adds a new element to the array void array :: add ( int num ) { if ( count < size ) { arr[count] = num ; count++ ; } else cout << " Array is full" << endl ; } the above code is used to inset of data in array. ii. Selection // displays elements in an array void array :: display( ) { for ( int i = 0 ; i < count ; i++ ) cout << arr[i] << "t" ; cout << endl ; } iii. Merge merge two arrays // merges two arrays void array :: merge ( array &a, array &b ) { sort ( a.arr, a.size ) ; sort ( b.arr, b.size ) ; size = a.count + b.count ; arr = new int[size] ; int i, j, k ; for ( i = j = k = 0 ; j < a.count || k < b.count ; ) { if ( a.arr[j] <= b.arr[k] ) arr[i++] = a.arr[j++] ; else
  • 12. arr[i++] = b.arr[k++] ; count++ ; if ( j == a.count || k == b.count ) break ; } for ( ; j < a.count ; ) { arr[i++] = a.arr[j++] ; count++ ; } for ( ; k < b.count ; ) { arr[i++] = b.arr[k++] ; count++ ; } }