合并有序数组
#include <stdio.h> typedef int object_t; size_t merge_sort(object_t* first, object_t* last, object_t* begin, object_t* end, object_t* result_first) { size_t len = last - first + (end - begin); object_t* result = result_first + len; while ((first != last) && (begin != end)) { if (*(last - 1) < *(end -1 )) { *(--result) = *(--end); } else { *(--result) = *(--last); } } while (first != last) { *(--result) = *(--last); } while (begin != end) { *(--result) = *(--end); } return len; } void array_print(object_t* first, object_t* last) { size_t len = last - first; if (len <= 0) { return; } while (first != last) { printf("%d\t", *first++); } printf("\n"); } int main(int argc, char *argv[]) { object_t memory_array[20]={ 1,5, 6, 16, 17 }; object_t station[] ={ 1, 3, 5, 6, 7 }; object_t* last1 = memory_array + 5; object_t* last2 = station + sizeof(station)/sizeof(object_t); array_print(memory_array, last1); array_print(station, last2); size_t len = merge_sort(memory_array, last1 , station, last2, memory_array); array_print(memory_array , memory_array + len); return 0; }