C++数组排序题求解

开发编程  |  电脑网络

对两个有序数组进行合并设有如下数组A、B,并假设两个数组的元素都已经有序(从大到小降序排列)。编写程序,合并A、B数组形成一个新的数组C,并使C的元素仍有序(从大到小降序排列)。int A[10]={123,88,86,86,33,15,7,0,-1,-3};int B[10]={86,69,51,50,27,19,15,12,5,2};
分享:
2012-01-03

2012-01-04最佳答案

#include <iostream.h>void main(){int A[10]={123,88,86,86,33,15,7,0,-1,-3}; int B[10]={86,69,51,50,27,19,15,12,5,2};int* C = new int[20];int* current = A;int* another = B;int* temp;int currentIndex = 0;int anotherIndex = 0;int tempIndex;for(int i = 0; i < 20; i++){if(anotherIndex != 10){if(currentIndex == 10 || current[currentIndex] < another[anotherIndex]){temp = current;current = another;another = temp;tempIndex = currentIndex;currentIndex = anotherIndex;anotherIndex = tempIndex;}}C[i] = current[currentIndex++];}}

其它1条答案

#include "stdafx.h" #include using namespace std; int main ( ) { int A[10]={200,129,88,86,33,15,7,0,-1,-3}, B[10]={28,87,51,50,27,19,15,12,5,1}; int i,j,C[20],p; for (i=0;i<=9;i ){ C[i]=A[i]; C[i 10]=B[i];} for (i=0;i<=19;i ){ for (j=i 1;j<=19;j ){ if (C[i]>C[j]){ p=C[i]; C[i]=C[j]; C[j]=p;}} cout<

2016-12-20