归并排序
来源:互联网 发布:劲舞团淘宝 编辑:程序博客网 时间:2024/06/02 00:39
读了别人的程序,自己重写了一遍,完整代码如下:
#include<iostream>using namespace std;#define MAXSIZE 10void print(int a[],int size = MAXSIZE){ int i;for(i=0;i<size;i++)cout<<a[i]<<" ";cout<<endl;}void merge_array(int a[],int first,int mid,int last,int temp[]){ if(first>mid||mid>last) return; int i,j,k; i=first; j=mid+1; k=0; while(i<=mid&&j<=last) { if(a[i]<=a[j]) temp[k++]=a[i++]; else temp[k++]=a[j++]; } while(i<=mid) temp[k++]=a[i++]; while(j<=last) temp[k++]=a[j++]; i=first; k=0; while(i<=last) a[i++]=temp[k++];}void merge_divide(int a[],int first,int last,int temp[]){ int mid; if(first<last) { mid = (first+last)/2; merge_divide(a,first,mid,temp); merge_divide(a,mid+1,last,temp); merge_array(a,first,mid,last,temp); }}void merge_sort(int a[],int size,int temp[]){ if(size<=1) return; merge_divide(a,0,size-1,temp);}int main(){ int a[MAXSIZE]={1,4,5,8,12,14,3,18,13,10}; int temp[MAXSIZE];merge_sort(a,MAXSIZE,temp);print(a);system("pause");return 1;}
参考地址:http://blog.csdn.net/morewindows/article/details/6678165#comments
- 归并排序-归并排序
- 归并和归并排序
- 归并与归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 排序::归并
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- VC++遍历目录
- 求一个程序!!急!!
- 基础算法系列(一)——选择排序
- Android之TelephonyManager&GsmCellLocation类的方法详解
- Linux下如何挂载U盘
- 归并排序
- 第10贴:DC—DC
- Source Insight 经典教程
- 微软等数据结构+算法面试100题(34)-- 类CMyString
- 监控卡驱动程序和软件的安装
- Delphi与c++ 数据类型对照表(从万一的博客园摘录)
- protel四层板制作及内电层分割技术
- 监控不能打开的判断问题
- 微软等数据结构+算法面试100题(35)-- 二叉树的深度