The two one-dimensional array merge

Has two rows of one-dimensional array of good order and merge maintain the same collation
1. The purpose of the experiment
to understand the order of the linear form of representation and implementation, and can utilize to solve more complex problems.
2 experimental content
known two integers sequence table is ordered, it will be merged into a single ordered sequence table.

It is assumed that two arrays are known LA = (3, 5, 8, 11), LB = (2, 6, 8, 9, 11, 15, 20), and are lined up in descending order of the rules.

Thinking Analysis :
first need to find a space to accommodate a larger array of two
such operations may be performed using pointer reapply space with malloc (), or realloc () function

/*malloc()函数使用*/
#include <stdlib.h>  //定义头文件
void *malloc(int size);  //函数声明

//使用方法
//例如分配100个存储空间
int *p;
p = (int *)malloc( sizeof(int) *100);

//详情请参照 (转载) https://blog.csdn.net/linan5231/article/details/50930630
/*  realloc()函数使用  */
#include <stdio.h>
void *realloc();

//使用方法
//例如将原来的100个空间变为200个
int *p;
p = (int *)realloc( sizeof(int) *200);

//详情请参照 (转载)https://blog.csdn.net/vevenlcf/article/details/47036127

There is no way of using pointers, direct re-apply to accommodate a new array list [11] of the original two arrays
is then inserted into the new array sort operation
on insertion sort, you can see the insertion sort Introduction

Here is the code of this question

#include <stdio.h>
int main()
{
	int i, j, temp;
	int list_a[4]={3,5,8,11}, list_b[7]={2,6,8,9,11,15,20};
	int list[11]={0};
	
	//输出原来的数组数据  并合并到一个新的更大的一维数组中 
	printf("The original order was:\n");
	printf("List A is: \n	");
	for(i=0;i<4;++i) 
	{
		list[i]=list_a[i];
		printf("%d  ",list[i]);
	}
	printf("\nList B is: \n	");
	for(i=4;i<11;++i)
	{
		list[i]=list_b[i-4];
		printf("%d  ",list[i]);
	}
	
	//对新的一维数组进行选择排序 
	for(i=1;i<11;++i)
	{
		temp=list[i];
		j=i-1;
		while(j>=0 && list[j]>temp)
		{
			list[j+1]=list[j];
			j=j-1;
		}
		list[j+1]=temp;
	}
	
	//将结果打印出来 
	printf("\n\n\nThe combined sort is:\n	");
	for(i=0;i<11;++i)
		printf("%d  ",list[i]);
	
	return 0;
}

Of course, this question can also be used merge sort, paste and other written out
to welcome everyone to provide better insights



The following updates are
two linear table sorted into one linear table, and sorted according to the original collation
data is still using the above data
pointer in the C language operation
detailed procedures, see code comments

#include <stdio.h>
#include <stdlib.h> //for using malloc()
int main() {
	int i=0, j=0;
	int La[4]= {3,5,8,11};// the first ordered list
	int Lb[7]= {2,6,8,9,11,15,20};//the second ordered list
	int *pa, *pb, *pc, *p; //pa for La  &&  pb for Lb  &&  pc for Lc (-the new list to restorage)
	pa=La;
	pb=Lb;
	pc = (int *)malloc(11 * sizeof(int)); //requesting dynamic space for the new list
	p=pc;  // p for working in Lc

	while(i < 4 && j < 7) {   //两个数组的公共长度部分
		if( *(pa+i) <= *(pb+j) )//对两个数组的元素进行大小比较
			*(p++)= *(pa+(i++));//如果pa + i 指向的元素较小,则将这个元素放到 Lc 的空间中,并将下标增加
		else
			*(p++)=*(pb+(j++));//反之则将pb + i 指向的元素放到 Lc 的空间中,并将下标增加
	}

	while(i<4) 
		*(p++)= *(pa+(i++));
/*
//经过上面的while循环后,i 的值并没有再次初始化为0 ,所以此时i 的值为while循环结束时的最后的值,如果此时 i 的值小于数组的长度,说明i 中还有元素剩余,将剩下的元素按顺序依次放入Lc 接下来的空间中即可 (注意,两个数组必定只有一个恰好达到而另一个没有)
*/

	while(j<7)
		*(p++)=*(pb+(j++));
/*
The reason is the same
*/

//将结果打印出来
	for(i=0; i<11; ++i)
		printf("%d  ",*(pc+i));

	return 0;
}
Published 22 original articles · won praise 39 · views 4049

Guess you like

Origin blog.csdn.net/weixin_44895666/article/details/101036951