归并排序
来源:互联网 发布:吉他软件电脑版 编辑:程序博客网 时间:2024/06/06 08:44
归并类
// MergeSort.h#include <iostream>template <typename T> class MergeSort{public: static void mergeSort(T *nums, int left, int right){ if (left >= right) return; int pos = (left + right) >> 1; mergeSort(nums, left, pos); mergeSort(nums, pos + 1, right); merge(nums, left, pos, right); }private: static void merge(T *nums, int left, int pos, int right){ int low = left; int high = pos+1; T *temp = new T[right - left + 1]; int idx = 0; while (low <= pos && high <= right){ if (nums[low] > nums[high]){ temp[idx++] = nums[high]; ++high; } else{ temp[idx++] = nums[low]; ++low; } } while (low <= pos){ temp[idx++] = nums[low++]; } while (high <= right){ temp[idx++] = nums[high++]; } for (int i = 0; i < idx; ++i){ nums[left + i] = temp[i]; } delete[] temp; }};
main func
#include <iostream>#include "MergeSort.h"using namespace std;int main(){ int data[] = { 12, 34, 54, 2, 576, 45, 8, 59 }; int len = sizeof(data) / sizeof(data[0]); MergeSort<int>::mergeSort(data, 0, len - 1); for (auto d : data){ std::cout << d << " "; } system("pause"); return 0;}
阅读全文
0 0
- 归并排序-归并排序
- 归并和归并排序
- 归并与归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 排序::归并
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 基本Kmeans算法介绍及其实现
- php常用字符串函数
- 基于netty的websocket服务
- Python学习——enumerate()函数的用法
- PHP 实现后缀表达式(接受四则运算字符串,输出计算结果,附代码)
- 归并排序
- 【C++】C++友元
- 例2-23 用一个随机序列模拟中国数字地震台网记录在频率域的振幅。中国数字地震台网的采样间隔是0.02s,运用通带频率是0-5Hz的低通理想滤波器对其进行滤波。数据长度是120s。模拟一下。
- 欢迎使用CSDN-markdown编辑器
- 使用VS2010创建windows窗体应用程序
- JAVA中关于new子类对象时是否创建父类
- JAVA数据结构笔记
- POJ
- 解决:firefox安装firebug版本不兼容