合并排序C语言实现
#include <stdio.h> #define LENA 10 #define LENB 5 void MergeSort( int * array1 , int len1 , int * array2 , int len2 ) { int i = 0 , j = 0 , k = 0; int data[LENA+LENB] = { 0 }; while ( k < LENA+LENB ) { if ( array1[i] < array2[j] ) { if ( i < len1 ) { data[k] = array1[i]; i++; } else { data[k] = array2[j]; j++; } } else { if ( j < len2 ) { data[k] = array2[j]; j++; } else { data[k] = array1[i]; i++; } } k++; } for ( int a = 0 ; a < k ; a++ ) { printf( "%d " , data[a] ); } printf( "\n" ); } void main() { int a[] = { 0 , 1 , 2 , 3 , 5 , 9 , 11 , 100 , 1000 , 9999 }; int b[] = { 9 , 11 , 100 , 300 , 1000 }; MergeSort( a , LENA , b , LENB ); }