基本算法-归并排序
来源:互联网 发布:制作手持身份证软件 编辑:程序博客网 时间:2024/05/22 12:29
归并排序:是一种较稳定的排序算法
时间复杂度:nlogn
基本思想:将一个数组进行拆分成两两一组,然后分别对每组进行排序组合。
基本算法:
//将有二个有序数列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
- 基本算法-归并排序
- 归并排序-基本算法
- 基本排序算法--归并排序
- 归并排序(基本思想以及算法实现)
- 基本算法——归并排序
- c/c++常用算法(12) -- 基本排序算法(归并排序)
- 排序-归并排序-基本
- 递归实现归并排序(基本排序算法)
- 【Python排序搜索基本算法】之归并排序
- 排序算法-归并排序
- 排序算法------归并排序
- 排序算法-归并排序
- 排序算法---归并排序
- 排序算法--归并排序
- 排序算法--归并排序
- 排序算法-归并排序
- 排序算法--归并排序
- 排序算法--归并排序
- 顺序表与链表的比较
- redhat6.3上安装oracle10g遇到的问题
- 第一篇
- 常见的链表操作
- 生成模型与判别模型
- 基本算法-归并排序
- UVA - 1585 Score
- android关于横竖屏切换
- 算法---全排列
- Swift UI专项训练35 UIAlertController
- VS2010添加类失败问题,弹出错误框,提示 CodeModel操作失败,无法访问标记数据库
- 从源码中深入学习Handler,HandlerThread,MessageQueue,Looper。
- 如何理解java的多态性
- android 各版本对应的sqlite版本