Skip to content

Instantly share code, notes, and snippets.

@wfwei
Created June 8, 2013 12:55
Show Gist options
  • Select an option

  • Save wfwei/5735100 to your computer and use it in GitHub Desktop.

Select an option

Save wfwei/5735100 to your computer and use it in GitHub Desktop.
归并有序数组
#include "stdio.h"
#include "stdlib.h"
int * merge(int*A, int *B, int len){
int *C = (int *)malloc(sizeof(int)*len*2);
int ai, bi, ci;
if(A==NULL || B==NULL || C==NULL)
return NULL;
for(ai=0, bi=0, ci=0; ai<len&&bi<len; ){
if(A[ai]<=B[bi])
C[ci++] = A[ai++];
else
C[ci++] = B[bi++];
}
while(ai<len)
C[ci++] = A[ai++];
while(bi<len)
C[ci++] = B[bi++];
return C;
}
int main()
{
int A[] = {2, 4, 5, 7}, B[]={1, 3, 3, 9}, len=4;
int *C = merge(A, B, len);
len += len;
while(len>0)
printf("%d ", C[--len]);
system("pause");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment