#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct IDX {
int *parray;
int len;
};
int main(int argc, char *argv[])
{
int array1[9] = { 1, 4, 8, 22, 34, 55, 89, 90, 100};
int array2[4] = { 3, 45, 78, 99};
int array3[7] = { 9, 32, 44, 74, 167, 244, 342};
int i, sum = 0, num, sidx, minix;
IDX idx[3];
idx[0].parray = array1;
idx[0].len = 9;
idx[1].parray = array2;
idx[1].len = 4;
idx[2].parray = array3;
idx[2].len = 7;
for (i = 0; i < 3; i++)
sum += idx[i].len;
int *mem_cache = (int *)malloc(sum * sizeof(int));
for (i = 0; i < sum; i++) {
//理论上num应该设为无穷大
num = 1000000;
for (sidx = 0; sidx < 3; sidx++) {
if ((idx[sidx].len > 0) && (*idx[sidx].parray < num)) {
num = *idx[sidx].parray;
minix = sidx;
}
}
mem_cache[i] = *idx[minix].parray;
idx[minix].parray++;
idx[minix].len--;
}
//打印输出
//从概念上来说,这里反复的使用i具有很好的时间局部性
for (i = 0; i < sum; i++) {
printf("%d ", mem_cache[i]);
}
free(mem_cache);
system("pause");
return 0;
}