剑指offer 归并排序
来源:互联网 发布:双向视觉优化训练系统 编辑:程序博客网 时间:2024/04/28 01:19
#include <iostream>using namespace std;//src is seperatly sorted [start,flag] and [flag+1,end]//merge 2 parts into 1 sorted array resvoid Merge(int* src,int* res,int start,int end,int flag){int i=start,j=flag+1,k=i;while(i<=flag && j<=end){if(src[i]<src[j]) res[k++]=src[i++];else res[k++]=src[j++];}if(i<=flag){while(i<=flag) res[k++]=src[i++];}if(j<=end){while(j<=end) res[k++]=src[j++];}}//get [start,end] sortedvoid MergeSortCore(int* src,int* res,int len,int start,int end){if(start==end){res[start]=src[start];return;}int tmpres[len];int flag=(start+end)/2;//get [start,flag] sortedMergeSortCore(src,tmpres,len,start,flag);//get [flag+1,end] sortedMergeSortCore(src,tmpres,len,flag+1,end);//merge 2 parts, to get [start,end] sortedMerge(tmpres,res,start,end,flag);}void MergeSort(int* src,int* res,int len){if(!src||!res||len<=0) return;MergeSortCore(src,res,len,0,len-1);}int main(){int src[]={3,4,6,5,1,2};int len=sizeof(src)/sizeof(*src);int res[len];MergeSort(src,res,len);for(int i=0;i<len;++i){cout<<res[i]<<' ';}}
0 0
- 剑指offer 归并排序
- 归并排序-剑指offer
- 剑指offer-归并排序
- 数组中的逆序对(归并排序+剑指offer)
- 剑指Offer——归并排序思想应用
- 剑指Offer--数组中的逆序对--归并排序!
- 码农小汪-剑指Offer之33 -数组中的逆序对 冒泡排序 归并排序
- 剑指offer之面试题36:数组中的逆序对-归并排序的应用
- 剑指offer——数组中的逆序数_____(归并排序)
- 剑指offer——数组中的逆序对(复习归并排序)
- 为了offer系列——归并排序(C++)
- 归并排序-归并排序
- 剑指offer中把数组排成最小数之归并排序实现(python 3.5.2)(附上归并和不开新数组的快排)
- 剑指Offer 36题 数组中的逆序对 Java版 这个和归并排序一起看 对比差别在哪
- 【剑指offer】快速排序
- 剑指offer--快速排序
- 剑指offer 快速排序
- 剑指offer 快速排序
- jquery加载js脚本
- 提高新股申购中签率——程序猿五招
- 指针与引用,这篇文章不错
- hdu 1496 hash+暴力
- 19周求a!+b!+c!
- 剑指offer 归并排序
- Qt 字体大小的计算
- 【大话设计模式】——行为型模式总结(罗列篇)
- Java Calendar获取时间的月和日、周
- <libxml/tree.h>找不到解决办法
- Linux下Qt Creator的工程配置Project Config
- 验证码识别的小程序源码
- mysql,hibernate Column '' not found
- 及时能在图片库中显示保存的图片