首页 > 代码库 > 二路归并排序
二路归并排序
void OnceSort(int *a, int size);void MergeSort(int *a, int size){ if (size <= 1) return ; int which = 2; int left = size/which; MergeSort(a, left); MergeSort(a + left, size - left); OnceSort(a, size);}void OnceSort(int *a, int size){ int len1 = size/2; int len2 = size-len1; int *L = new int[len1]; int *R = new int[len2]; int j = 0; int k = 0; int i = 0; for (i = 0; i < len1; i++) { L[i] = a[i]; } for (i = len1; i < size; i++) { R[i-len1] = a[i]; } for ( i = 0, j = 0, k = 0; k < size; k++) { if( i >= len1 || (j < len2 && L[i] > R[j] )) { a[k] = R[j]; j++; } else { a[k] = L[i]; i++; } } delete []L; delete []R;}
二路归并排序
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。