Code Monkey home page Code Monkey logo

dailycode's Introduction

//合并两有序数组
#include "pch.h"
#include <iostream>

void showArray(int as[],int length);

int * merge(int a1[], int a2[], int length1,int length2)
{
	int * result = (int *)malloc((length1 + length2) * sizeof(int));
	//注意长度和索引对比的时候,长度应该-1
	int index1 = 0 , index2 = 0 , i = 0 ;
	for (i = 0; i < length1+length2; i++)
	{
		if (a1[index1] < a2[index2] || index2 >= length2)
		{
			result[i] = a1[index1++];
		}
		else if (a1[index1] >= a2[index2] || index1 >= length1)
		{
			result[i] = a2[index2++];
		}
	}
	return result;
}

int main()
{
	int a1[6] = { 1,6,8,9,55,90 };
	int a2[3] = { 1,33,44 };
	int * c = merge(a1, a2, 6, 3);
	showArray(c, 9);
}

void showArray(int as[],int length)
{
	for (int i = 0; i < length; i++)
	{
		printf("%d\t", as[i]);
	}
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.