归并排序
来源:互联网 发布:软件工作室 编辑:程序博客网 时间:2024/06/04 20:15
归并排序
点击打开链接
//将有二个有序数列a[first...mid]和a[mid...last]合并。 void mergearray(int a[], int first, int mid, int last, int temp[]) { int i = first, j = mid + 1; int m = mid, n = last; int k = 0; while (i <= m && j <= n) { if (a[i] <= a[j]) temp[k++] = a[i++]; else temp[k++] = a[j++]; } while (i <= m) temp[k++] = a[i++]; while (j <= n) temp[k++] = a[j++]; for (i = 0; i < k; i++) a[first + i] = temp[i]; } void mergesort(int a[], int first, int last, int temp[]) { if (first < last) { int mid = (first + last) / 2; mergesort(a, first, mid, temp); //左边有序 mergesort(a, mid + 1, last, temp); //右边有序 mergearray(a, first, mid, last, temp); //再将二个有序数列合并 } } bool MergeSort(int a[], int n) { int *p = new int[n]; if (p == NULL) return false; mergesort(a, 0, n - 1, p); delete[] p; return true; }
0 0
- 归并排序-归并排序
- 归并和归并排序
- 归并与归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 排序::归并
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- jquery select 一些常见的方法
- UICollectionView使用 - iOS开发
- 【view】android中onMeasure初看,深入理解布局之一!
- 缓存淘汰算法系列之1——LRU类
- 我对百度云盘的理解及使用
- 归并排序
- 获取系统当前日期时间
- 下一代ERP
- epp3切换字符集
- python的开发环境
- 最小生成树算法(Prime、Kruskal)和最短路径算法(Dijkstra、Floyd)
- UML图中类之间的关系:依赖,泛化,关联,聚合,组合,实现
- GCC编译引入第三方库的方法
- Fedora环境下Gtk+开发环境配置