static int MergeCount(int *arr, int start, int end){ int acc = 0; if (end - start <= 1) return acc; int mid = (start + end)/2; int leftCount = MergeCount(arr, start, mid); int rightCount = MergeCount(arr, mid, end); if (mid < end && mid >= 1){ if (arr[mid -1] > arr[mid]){ acc++; int compare = arr[mid -1]; for(int i = mid + 1; i < end; i++){ if (compare > arr[mid]){ acc++; } } } } return acc + leftCount + rightCount; }
int32_t MergeFunc(MEM_POOL_PTR mem_pool, enum function_type agr_type, struct low_data_struct *cur_value, struct low_data_struct *new_value) { switch (agr_type) { case FUNC_MIN: return MergeMin(mem_pool, cur_value, new_value); case FUNC_MAX: return MergeMax(mem_pool, cur_value, new_value); case FUNC_COUNT: return MergeCount(mem_pool, cur_value, new_value); case FUNC_SUM: return MergeSum(mem_pool, cur_value, new_value); default: log_error("func type %d not supported for merge.", agr_type); return ERROR_UNSUPPORTED_AGRFUNC_TYPE; } }
int main(){ int A[] = {1, 2, 5, 3, 4}; printf("MergeCount: %d\n", MergeCount(A, 0, 1)); }