二路归并算法(c语言)
#include <stdio.h> #include <stdlib.h> void merge(int* data, int l,int m,int r)//l 左端起始点,m为中间点,r为右端点 { int left = l; int right = m; int n = r -l; int* tmp = (int*) malloc(sizeof(int)*n); int count = 0; while (left < m && right <r ) { if (data[left] <= data[right]) { tmp[count] = data[left]; left ++; } else { tmp[count] = data[right]; right ++; } count ++; } while (left < m) { tmp[count] = data[left]; count++; left++; } while (right < r) { tmp[count] = data[right]; count++; right++; } for (int i = 0; i < r; i++) { data[i] = tmp[i]; } free(tmp); } int main() { int data[12] = {1,3,5,6,9,12,0,2,3,4,8,88}; merge(data,0,6,12); for (int i = 0; i < 12; i++) { printf("%d ",data[i]); } return 0; }
原文:https://www.cnblogs.com/jackzone/p/8975038.html