浅谈二路归并排序
来源:互联网 发布:淘宝手机市场 编辑:程序博客网 时间:2024/06/05 20:26
二路归并排序:
把待排序序列分为两个子序列,对每个子序列分别进行二路归并排序,再把有序的两个子序列合并为整体有序序列。
public static void mergeSort(int[] nums, int low, int high) {int mid = (low + high) / 2;if (low < high) { mergeSort(nums, low, mid); // 左边归并排序 mergeSort(nums, mid + 1, high); // 右边归并排序 merge(nums, low, mid, high); // 左右两边归并为有序序列 }}public static void merge(int[] nums, int low, int mid, int high) {int[] temp = new int[high - low + 1]; // 定义一个新数组存储归并结果 int i = low; // 左指针 int j = mid + 1; // 右指针 int k = 0;// 将左边和右边对应位置的数比较,较小的先移到新数组中while (i <= mid && j <= high) { if (nums[i] < nums[j]) temp[k++] = nums[i++]; else temp[k++] = nums[j++];} // 把左边剩余的数移入新数组while (i <= mid) temp[k++] = nums[i++]; // 把右边剩余的数移入新数组while (j <= high) temp[k++] = nums[j++]; // 把新数组复制回原数组 for (int p = 0; p < temp.length; p++) nums[p + low] = temp[p];}
最好时间复杂度:O(nlogn)
最坏时间复杂度:O(nlogn)
平均时间复杂度:O(nlogn)
1 0
- 浅谈二路归并排序
- 归并排序:二路归并
- 归并排序--二路归并
- 二路归并排序
- 二路归并排序
- 二路归并排序
- 二路归并排序
- 二路归并排序
- 二路归并排序
- 二路归并排序
- 二路归并排序
- 二路归并排序
- 二路归并排序
- 二路归并排序
- 二路归并排序
- 二路归并排序
- 二路归并排序
- 二路归并排序
- docker 相关操作
- [LPE]Windows Local Privilege Escalation MS16-135
- 图像拼接(九):双摄像头实时视频拼接(单应变换模型)
- Asp.Net 常用工具类之加密——非对称加密RSA算法
- repo&git&gerrit联系和使用
- 浅谈二路归并排序
- Android EventBus实战
- 如何实现大数据价值最大化?且听这位委员的建议
- 利用Blob, a.download, URL.createObjectURL模拟下载文件
- Dmaven.multiModuleProjectDirectory system propery is not set.
- oracle函数命令
- PAT_A 1125. Chain the Ropes (25)
- 导出excl
- CORS(跨域资源共享)