归并
来源:互联网 发布:ubuntu镜像文件下载 编辑:程序博客网 时间:2024/04/27 18:31
package sort;
public class MergeSort {
public void mergearray(int a[],int first,int mid,int last,int temp[]){//合并两个有序序列,合并后依然有序。
int i=first;
int j=mid;
int m=mid+1;
int n=last;
int k=0;
while(i<=j&&m<=n){
if(a[i]<a[m]){
temp[k++]=a[i++];
}else{
temp[k++]=a[m++];
}
}
while(i<=j){
temp[k++]=a[i++];
}
while(m<=n){
temp[k++]=a[m++];
}
for(int p=0;p<k;p++){
a[first+i]=temp[i];
}
}
public 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);//合并
}
}
}
public class MergeSort {
public void mergearray(int a[],int first,int mid,int last,int temp[]){//合并两个有序序列,合并后依然有序。
int i=first;
int j=mid;
int m=mid+1;
int n=last;
int k=0;
while(i<=j&&m<=n){
if(a[i]<a[m]){
temp[k++]=a[i++];
}else{
temp[k++]=a[m++];
}
}
while(i<=j){
temp[k++]=a[i++];
}
while(m<=n){
temp[k++]=a[m++];
}
for(int p=0;p<k;p++){
a[first+i]=temp[i];
}
}
public 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);//合并
}
}
}
0 0
- 归并
- 归并
- 归并
- 归并排序-归并排序
- 归并和归并排序
- 归并与归并排序
- 二路归并 && 插入归并 && 原地归并
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 排序::归并
- 归并排序
- 归并算法
- 归并排序
- 归并排序
- poj 3984
- 递推递归练习 P
- HDU2033 人见人爱A+B
- Android O 开发者预览版新特性
- scrapy爬虫(以东方烟草网为例)
- 归并
- 采药 动态规划
- 跨站的艺术-XSS Fuzzing 的技巧
- [BZOJ2813]奇妙的Fibonacci(线性筛)
- Android 案例—装备选择
- bzoj3994[SDOI2015]约数个数和
- maven中scope标签详解
- Lintcode 木材加工
- 数据结构(1)-线性表(顺序存储结构)