合并排序算法 在一个条件下合并第三个数组中的两个数组
Merge Sort Algorithm merging of two a arrays in third on a condition
这是一种在特定条件下合并两个数组的算法。 它首先比较两个数组的索引,如果 arrayP1 的索引等于 arrayP2 的索引,它将该特定索引存储到答案数组中。任何人都可以帮助在C ++中翻译此算法吗
#include <iostream>
#include <cstdlib>
#include <conio.h>
using namespace std;
int main()
{
int sizeofarray=0,i=0 ,j=0, num=0, answers[]={};
cout<<"enter the size of array"<<endl;
cin>>sizeofarray;//take size of array from user
int array1[sizeofarray];
int array2[sizeofarray];
cout<<"please enter a sorted array member of Array1"<<endl;
//input of array element
for ( i=0 ; i<=sizeofarray; i++)
{
cin>>array1[i];
}
system("CLS");
cout<<"please enter a sorted array member of Array2"<<endl;
for (j=0 ; j<=sizeofarray; j++)
{
cin>>array2[j];
}
***strong text***system("CLS");
//comparing the array element and storing the similar items to another array
while(array1[i]!=NULL && array2[j]!=NULL){
if(array1[i]==array2[j]){
answer[num++]=array1[i];
i++;
j++;
}
else if(array1[i]<array2[j])
{
i++;
}else{
j++;
}
i++;
j++;
}
cout<<"The number of common elements"<<num<<endl;
cout<<"These are the common numbers: ";
for (int k=0;k<num;k++){
cout<<answer[k]<<" ";
}
getch();
return 0;
}
#include <iostream>
#include <cstdlib>
#include <conio.h>
using namespace std;
int main()
{
int sizeofarray=0,i=0 ,j=0, num=0, answers[]={};
cout<<"enter the size of array"<<endl;
cin>>sizeofarray;//take size of array from user
int array1[sizeofarray];
int array2[sizeofarray];
cout<<"please enter a sorted array member of Array1"<<endl;
//input of array element
for ( i=0 ; i<=sizeofarray; i++)
{
cin>>array1[i];
}
system("CLS");
cout<<"please enter a sorted array member of Array2"<<endl;
for (j=0 ; j<=sizeofarray; j++)
{
cin>>array2[j];
}
system("CLS");
//comparing the array element and storing the similar items to another array
while(array1[i]!=NULL && array2[j]!=NULL){
if(array1[i]==array2[j]){
answer[num++]=array1[i];
i++;
j++;
}
else if(array1[i]<array2[j])
{
i++;
}else{
j++;
}
i++;
j++;
}
cout<<"The number of common elements"<<num<<endl;
cout<<"These are the common numbers: ";
for (int k=0;k<num;k++){
cout<<answer[k]<<" ";
}
getch();
return 0;
}
相关文章:
- 合并排序不排序自创建数组类 c++
- 数组为此合并排序函数提供了正确的输出,但向量给出了不正确的输出.出了什么问题?
- 我如何根据 c++ 中的行合并两个 2D 数组
- 使用基于数组的列表 c++ 合并 2 个数组
- 如何在 char 数组中合并两个数组以通过网络发送
- 合并数组的程序打印 0 而不是实际合并的数组
- 如果数组长度为 100000,则使用合并排序对反转进行计数会给出负数
- 合并两个数组后数组大小不正确
- 如何修复合并两个C++数组时'Invalid Pointer'
- 在OpenMP上的并行合并排序上的数组大小问题.如何将合并分类分类为更多任务
- C++全局常量数组:是否保证合并(优化)到一个副本中
- C - 为什么合并函数在递归调用后会逆转数组
- 合并排序 - 返回新数组,而不是将合并的数组复制到输入数组
- C MPI:STD ::在数组上合并
- 合并排序问题,在方法之间传递数组
- C 合并的数组输出错误
- 我如何在INT数组上实现合并排序
- C++将2个已排序的数组合并为1个已排序数组
- 将排序代码与数组合并,陷入合并,只需要一点点修正
- 将字符* 数组合并到uint16_t