#include<stdio.h>
#include<string.h>
/*
func: 合并两个有序数组
arrSrc:将另一有序数组arrDest合并至该数组
arrDest:将该数组合并至另一有序数组arrSrc
captionSrc:arrSrc的容量大小,保证能保存arrSrc + arrDest
lenSrc:arrSrc数组的现有有效元素个数
lenDest:arrDest数组的现有有效元素个数
*/
void mergeArray(int* arrSrc, int* arrDest,int captionSrc,int lenSrc, int lenDest)
{
if(NULL == arrSrc || NULL == arrDest || ((lenSrc + lenDest) > captionSrc)) {
return;
}
int indexOfNew = lenSrc + lenDest - 1; //新数组的末位索引
int indexOfSrc = lenSrc - 1;//arrSrc有效元素的末位索引
int indexOfDest = lenDest - 1;//arrDest有效元素的末位索引
//当arrDest全部扫描完成,元素全部插入arrSrc,剩余arrSrc元素不需要移位操作
//当arrSrc全部遍历完成,但arrDest仍有元素时,只需要操作arrDest即可,此时arrSrc下标已达最小值0
while (indexOfDest >= 0) {
if (indexOfSrc >= 0){
C语言合并两个有序数组,时间复杂度为O(n),空间复杂度O(1)
最新推荐文章于 2024-05-07 16:51:53 发布