سلام دوستان ، این برنامه که تو سی پلاس پلاس نوشتم ، دو آرایه نامرتب را گرفته و آنها را
بصورت مرتب در یک آرایه دیگر قرار میدهد !!!
کد برنامه تقدیم شما عزیزان :
#include <iostream>
#include <conio>
#include <algorithm>
void swap(int &a,int &b){
int temp;
temp=a; a=b; b=temp;
}
void Sort(int a[],int n){
int max=0;
for(int i=n-1;i>=0;i--){
for(int j=0;j<=i;j++){
if(a[j]>a[max])
max=j;}
swap(a[i],a[max]);
max=0;}
}
void Merg(int a[],int m,int b[],int n){
int *c=new int[n+m];
for(int i=0;i<m;i++){
c[i]=a[i];
}
for(int j=0;j<n;j++){
c[m+j]=b[j];
}
//Now Sorting Array C
Sort(c,n+m);
for(int k=0;k<n+m;k++)
cout<<c[k]<<" ";
//------------------------------------------------------------------------------
}
int main(){
int m,n;
cout<<"Enter size of Array 1:\n";
cin>>m;
cout<<"Enter size of Array 2:\n";
cin>>n;
//---- Building dynamic Arrays----
int *a=new int[m];
int *b=new int[n];
//--------------------------------
cout<<"Enter Array 1:\n";
for(int i=0;i<m;i++)
cin>>a[i];
cout<<"Enter Array 2:\n";
for(int i=0;i<n;i++)
cin>>b[i];
cout<<"---------------------------\n";
//Merging Two Arrays
cout<<"*** Sorted Merged Array ***\n\n";
Merg(a,m,b,n);
getch();
}
موفق و پیروز باشید !!!