#include
#include
#include
#include
using namespace std;
const int M=5;
const int N=6;
void sort(int[],int);
void merge(int*,int*,int*,int,int);
int main(){
int i,a[M],b[N],c[M+N];
srand((unsigned int)time(NULL));
cout<
sort(a,M);
for(i=0;i
sort(b,N);\
for(i=0;i
cout<
}
void sort(int a[],int n){
for(int i=0;i
int t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
void merge(int *p,int *q,int *k,int m,int n){
int *a=p,*b=q;
while(p if(*p<*q){
*k=*p;
p++;
}
else{
*k=*q;
q++;
}
k++;
}
while(p *k=*p;
p++;
k++;
}
while(q *k=*q;
q++;
k++;
}
}
#include
using namespace std;
void DisplayArray(int *pArray, int nLen)
{
for (int i = 0; i < nLen; ++i)
{
cout<< pArray[i]<<" ";
}
cout<
// pArray1和pArray2是已经排好序的数组,要求将它们按照顺序合并到pArray中
// 排序之后的数组不会有重复的元素
void MergeArray(int *pArray1, int nLen1, int *pArray2, int nLen2, int *pArray)
{
int i, j, n;
i = j = n = 0;
while (i < nLen1 && j < nLen2)// 循环一直进行到拷贝完某一个数组的元素为止
{
if (pArray1[i] < pArray2[j])// 拷贝array1的元素
{
pArray[n++] = pArray1[i++];
}
else if (pArray1[i] > pArray2[j])// 拷贝array2的元素
{
pArray[n++] = pArray2[j++];
}
else // 相等的元素拷贝
{
pArray[n++] = pArray2[j++];
++i;
}
}
if (i == nLen1)// 如果array1已经被拷贝完毕就拷贝array2的元素
{
while (j < nLen2)
pArray[n++] = pArray2[j++];
}
else// 如果array2已经被拷贝完毕就拷贝array1的元素
{
while (i < nLen1)
pArray[n++] = pArray1[i++];
}
}
int main()
{
int array1[] = {1, 4, 5, 7};
int array2[] = {2, 3, 6, 8};
int array3[8];
MergeArray(array1, 4, array2, 4, array3);
cout<<"Array 1:";
DisplayArray(array1, 4);
cout<<"Array 2:";
DisplayArray(array2, 4);
cout<<"Merge Array:";
DisplayArray(array3, 8);
return 1;
}
#include
using namespace std;
void fun(int *a1,int size1,int *a2,int size2)
{
int *a = new int[size1 + size2];
int temp = size1;
for (int i = 0;i < size1;i++)
a[i] = a1[i];
for (int i = 0;i < size2;i++,temp++)
a[temp] = a2[i];
for (int i = 0;i < size1 + size2 - 1;i++)
for (int j = i + 1;j < size1 + size2;j++)
if (a[j] < a[i])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
for (int i = 0;i < size1 + size2;i++)
cout << a[i] << " ";
cout << endl;
delete [] a;
}
int main()
{
int a[] = {1,3,6,9,12};
int b[] = {2,4,8,10,16};
fun(a,5,b,5);
return 0;
}
#include
void pxsort(int *c,int n)
{
for(int j=0;j
{ int temp=c[i];
c[i]=c[i+1];
c[i+1]=temp;
}
}
void main()
{
int i,j,a[5],b[5],c[10];
cout<<"第一个数组";
for(i=0;i<5;i++)
cin>>a[i];
cout<<"第2个数组";
for(i=0;i<5;i++)
cin>>b[i];
for(i=0;i<5;i++)
{
c[i]=a[i];
c[i+5]=b[i];
}
cout<
for(i=0;i<10;i++)
cout<
输入两个数组,
合并后输出
void f(int *a1, int n, int *a2, int m, int *a_out){
int l1,l2;
l1=l2=0;
for(int i=0;i
else
a_out[i]=a2[l2++];
else if(l1>=n)
a_out[i]=a2[l2++];
else
a_out[i]=a1[l1++];
}
}