1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| void merge_arrays(const int* src_array, int* const dest_array, const int num_lists, const int num_elements){
const int num_element_per_list = (num_elements/num_lists);
int list_indexes[5]; for (int l=0; l<num_lists; l++){ list_indexes[l] = 0; }
for (int i=0; i<num_elements; i++){ dest_array[i] = find_min(src_array, list_indexes, num_lists, num_element_per_list); } return; }
int find_min(const int* src_array, int* const list_indexes, const int num_lists, const int num_elements_per_list){ int min_val = INT_MAX; int min_idx = 0; for (int i=0; i<num_lists; i++){ if (list_indexes[i] < num_elements_per_list){ const int src_idx = list_indexes[i] + num_elements_per_list * i; const int data = src_array[src_idx]; if (data <= min_val){ min_val = data; min_idx = i; } } } list_indexes[min_idx]++; return min_val; }
int main(){ vector<int> tmp = { 2,4,7, 1,90,91, 3,5,7, 10,11,13, 8,9,12}; }
|