归并排序
来源:互联网 发布:淘宝手柄 编辑:程序博客网 时间:2024/04/27 11:02
public class MergeSort {
public static void main(String[] args)
{
int a[]={1,3,0,5,6};
MergeSort ms=new MergeSort();
ms.sort (a,0,4);
for (int i=0;i<a.length;i++)
System.out.println(a[i]);
}
public void sort(int a[],int low,int high)
{
if (low<high)
{
int mid=(low+high)/2;
sort(a,low,mid);
sort(a,mid+1,high);
mergesort(a,low,high,mid);
}
}
public void mergesort(int a[],int low,int high,int mid)
{
int c[]=new int [a.length];
int lowtemp=low;
int midtemp=mid+1;
int tempadr=low;
int tmp=low;
while (lowtemp<=mid&&midtemp<=high)
{
if (a[lowtemp]<=a[midtemp])
c[tempadr++]=a[lowtemp++];
else
c[tempadr++]=a[midtemp++];
}
while (midtemp<=high)
c[tempadr++]=a[midtemp++];
while(lowtemp<=mid)
c[tempadr++]=a[lowtemp++];
for (int i=low;i<=high;i++)
a[i]=c[i];
System.out.println(Arrays.toString(a));
}
}
public static void main(String[] args)
{
int a[]={1,3,0,5,6};
MergeSort ms=new MergeSort();
ms.sort (a,0,4);
for (int i=0;i<a.length;i++)
System.out.println(a[i]);
}
public void sort(int a[],int low,int high)
{
if (low<high)
{
int mid=(low+high)/2;
sort(a,low,mid);
sort(a,mid+1,high);
mergesort(a,low,high,mid);
}
}
public void mergesort(int a[],int low,int high,int mid)
{
int c[]=new int [a.length];
int lowtemp=low;
int midtemp=mid+1;
int tempadr=low;
int tmp=low;
while (lowtemp<=mid&&midtemp<=high)
{
if (a[lowtemp]<=a[midtemp])
c[tempadr++]=a[lowtemp++];
else
c[tempadr++]=a[midtemp++];
}
while (midtemp<=high)
c[tempadr++]=a[midtemp++];
while(lowtemp<=mid)
c[tempadr++]=a[lowtemp++];
for (int i=low;i<=high;i++)
a[i]=c[i];
System.out.println(Arrays.toString(a));
}
}
0 0
- 归并排序-归并排序
- 归并和归并排序
- 归并与归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 排序::归并
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- JS执行顺序-(deferred 延迟队列)
- 网站检测该注意的点
- Java生成UUID
- 《windows核心编程系列》谈谈内存映射文件
- Ubuntu 12.10 软件更新源列表
- 归并排序
- C/C++实现多参数函数编程
- 拉格朗日对偶
- C语言需要注意的基础知识点(兰亭风雨)
- 高性能MySQL之事务
- 多线程的那点儿事(之数据同步)
- Mali GPU OpenGL ES 应用性能优化--测试+定位+优化流程
- 卷一 内核源代码分析 第一章 linux内核对cortex A9多核处理器的支持 1.1 CPU的基本管理 图书试读版-请勿转载
- 关联对象 Associated Object