Search
 
SCRIPT & CODE EXAMPLE
 

CPP

merge sort in descending order c++

#include<iostream>
using namespace std;

void Merge(int A[], int B[], int C[], int N, int M, int &K);

int main()
{
	int A[100], B[100], C[200],i,n,m,k;

	cout<<"
Enter number of elements you want to insert in first array ";
	cin>>n;

	cout<<"Enter element in descending order
";

	for(i=0;i<n;i++)
	{
		cout<<"Enter element "<<i+1<<":";
		cin>>A[i];
	}

	cout<<"
Enter number of elements you want to insert in second array ";
	cin>>m;

	cout<<"Enter element in descending order
";

	for(i=0;i<m;i++)
	{
		cout<<"Enter element "<<i+1<<":";
		cin>>B[i];
	}

	Merge(A,B,C,n,m,k);

	cout<<"
The Merged Array in Descending Order"<<endl;

	for(i=0;i<k;i++)
	{
		cout<<C[i]<<" ";
	}

	return 0;
}

void Merge(int A[], int B[], int C[], int N, int M, int &K)
{
	int I=0, J=0;
	K=0;
	
	while (I<N && J<M)
	{
		if (A[I]>B[J])
			C[K++]=A[I++];
		else if (A[I]<B[J])
			C[K++]=B[J++];
		else
		{
			C[K++]=A[I++];
			J++;
		}
	}

	for (int T=I;T<N;T++)
		C[K++]=A[T];

	for (int T=J;T<M;T++)
		C[K++]=B[T];
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: c++ pass function as argument 
Cpp :: How to generate all the possible subsets of a set ? 
Cpp :: stack data structure c++ 
Cpp :: ex: cpp 
Cpp :: invert a binary tree 
Cpp :: javascript if else exercises 
Cpp :: declare class c++ 
Cpp :: c++ main function parameters 
Cpp :: c++ last element of vector 
Cpp :: C++ insert character 
Cpp :: c++ string example 
Cpp :: double plus overload 
Cpp :: check if cin got the wrong type 
Cpp :: c++ download 
Cpp :: c++c 
Cpp :: C# adding numbers 
Cpp :: 1822. Sign of the Product of an Array leetcode in c++ 
Cpp :: c++ exeption handling 
Cpp :: c++ camera capture 
Cpp :: turbo c++ easy programs 
Cpp :: print float up to 3 decimal places in c++ 
Cpp :: c ++ Prefix Sum of Matrix (Or 2D Array) 
Cpp :: how to change the icon of an exe in c++ 
Cpp :: dinamic 
Cpp :: how to print out a two dimensional array in c++ 
Cpp :: how to modify set C++ 
Cpp :: how to compile with libstdc++ fedora 
Cpp :: C++ selectin file location using Win32 API 
Cpp :: shrek c++ 
Cpp :: 1/2(-3-3)(-3+4) 
ADD CONTENT
Topic
Content
Source link
Name
1+8 =