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