/*
姓名:Gao;
程序名称:两有序顺序表合为一个有序顺序表
*/
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
	//
	//变量定义
	int L1, L2,L3,i,j,k,min,temp;
	int* p1=NULL, * p2=NULL,*p3=NULL;
	//变量定义结束
	///
	//指针内存初始化
	printf("L1:");
	scanf_s("%d", &L1);
	getchar();
	printf("L2:");
	scanf_s("%d", &L2);
	getchar();
	L3 = L1 + L2;
	p1 = (int*)malloc(sizeof(int) * L1);
	p2 = (int*)malloc(sizeof(int) * L2);
	p3 = (int*)malloc(sizeof(int) * L3);
	if (p1 == NULL || p2 == NULL || p3 == NULL)
		return 0;
	//指针内存初始化结束
	
	//两顺序表元素的输入
	printf("please input L1\n");
	for (i = 0; i < L1; i++)
	{
		scanf_s("%d", p1 + i);
		getchar();
	}
	printf("please input L2\n");
	for (i = 0; i < L2; i++)
	{
		scanf_s("%d", p2 + i);
		getchar();
	}
	//两顺序表元素输入完成
	///
	//两顺序表元素的大小排序
	//L1排序:
	for (i = 0; i < L1; i++)
	{
		for (j = i+1; j < L1; j++)
		{
			if ((*(p1+i))> (* (p1 + j)))
			{
				temp = *(p1 + i);
				*(p1 + i) = *(p1 + j);
				*(p1 + j) = temp;
			}
		}
	}
	//L1的输出:
	printf("L1:");
	for (i = 0; i < L1; i++)
		printf("%d ", *(p1 + i));
	printf("\n");

	//L2排序:
	for (i = 0; i < L2; i++)
	{
		for (j = i+1; j < L2; j++)
		{
			if (*(p2 + i) > * (p2 + j))
			{
				temp = *(p2 + i);
				*(p2 + i) = *(p2 + j);
				*(p2 + j) =temp;
			}
		}
	}
	//L2的输出:
	printf("L2:");
	for (i = 0; i < L2; i++)
		printf("%d ", *(p2 + i));
	printf("\n");
	//排序结束
	///
	//将L1与L2合并到L3内:
	i = 0;
	j = 0;
	for (k = 0; k < L3; k++)
	{
		if (*(p1 + i) < *(p2 + j))
		{
			*(p3 + k) = *(p1 + i);
			i++;
		}
		else
		{
			*(p3 + k) = *(p2 + j);
			j++;
		}
		if (i == L1+1)
		{
			for (j; j < L2; j++)
			{
				*(p3 + k) = *(p2 + j);
				k++;
			}
			break;
		}
		if (j == L2+1)
		{
			for (i; i < L1; i++)
			{
				*(p3 + k )= *(p1 + i);
				k++;
			}
			break;
		}



	}
	//L3的输出:
	printf("L3:");
	for (i = 0; i < L3; i++)
		printf("%d ", *(p3 + i));
	printf("\n");
	//释放内存:
	free(p1);
	free(p2);
	free(p3);

	return 0;
}