《算法导论》归并排序----merge-sort
伪代码请见《算法导论》2.3节
merge-sort实现:
public class MergeSort { public static void sort(double [] A,int p, int r) { if(p<r) { int q = (int) Math.floor( (p+r)/2 ); sort(A,p,q); sort(A,q+1,r); merge(A,p,q,r); } return ;
} public static void merge(double [] A, int p, int q, int r) { int n1 = q-p+1; int n2 = r-q; double [] L = new double[n1]; double [] R = new double[n2]; for(int i=0; i<n1; i++) L[i] = A[p+i]; for(int i=0; i<n2;i++) R[i]=A[q+i+1]; int i=0; int j=0; int counter = p; while(i<L.length && j<R.length) { if(L[i]<=R[j]) { A[counter]=L[i]; i++; } else { A[counter]=R[j]; j++; } counter++; } if(i==L.length) { for(int k=j;k<R.length;k++) A[counter++] = R[k]; } else { for(int k=i;k<L.length;k++) A[counter++] = L[k]; } } public static void main(String[] args) { // TODO Auto-generated method stub double [] A = {1.3, 5 ,2, 6.9, 2.0,7.8,4.3}; MergeSort.sort(A,0,A.length-1); for(double a:A) System.out.print(a+" "); }}
《算法导论》归并排序----merge-sort的更多相关文章
- 经典排序算法 - 归并排序Merge sort
经典排序算法 - 归并排序Merge sort 原理,把原始数组分成若干子数组,对每个子数组进行排序, 继续把子数组与子数组合并,合并后仍然有序,直到所有合并完,形成有序的数组 举例 无序数组[6 2 ...
- 排序算法二:归并排序(Merge sort)
归并排序(Merge sort)用到了分治思想,即分-治-合三步,算法平均时间复杂度是O(nlgn). (一)算法实现 private void merge_sort(int[] array, int ...
- 连续线性空间排序 起泡排序(bubble sort),归并排序(merge sort)
连续线性空间排序 起泡排序(bubble sort),归并排序(merge sort) 1,起泡排序(bubble sort),大致有三种算法 基本版,全扫描. 提前终止版,如果发现前区里没有发生交换 ...
- 归并排序(merge sort)
M erge sort is based on the divide-and-conquer paradigm. Its worst-case running time has a lower ord ...
- STL 源代码剖析 算法 stl_algo.h -- merge sort
本文为senlie原创.转载请保留此地址:http://blog.csdn.net/zhengsenlie merge sort ----------------------------------- ...
- 归并排序——Merge Sort
基本思想:参考 归并排序是建立在归并操作上的一种有效的排序算法.该算法是采用分治法的一个非常典型的应用.首先考虑下如何将2个有序数列合并.这个非常简单,只要从比较2个数列的第一个数,谁小就先取谁,取了 ...
- 归并排序Merge Sort
//C语言实现 void mergeSort(int array[],int first, int last) { if (first < last)//拆分数列中元素只剩下两个的时候,不再拆分 ...
- 归并排序Merge sort(转)
原理,把原始数组分成若干子数组,对每一个子数组进行排序, 继续把子数组与子数组合并,合并后仍然有序,直到全部合并完,形成有序的数组 举例 无序数组[6 2 4 1 5 9] 先看一下每个步骤下的状态, ...
- 数据结构 - 归并排序(merging sort)
归并排序(merging sort): 包含2-路归并排序, 把数组拆分成两段, 使用递归, 将两个有序表合成一个新的有序表. 归并排序(merge sort)的时间复杂度是O(nlogn), 实际效 ...
- 数据结构 - 归并排序(merging sort) 具体解释 及 代码
归并排序(merging sort) 具体解释 及 代码 本文地址: http://blog.csdn.net/caroline_wendy 归并排序(merging sort): 包括2-路归并排序 ...
随机推荐
- USACO Section 1.3 Ski Course Design 解题报告
题目 题目描述 有N座山,每座山都有一个高度,现在由于农夫想避税,所以想把这些山的高度进行一些改变,使得最高的山与最低的山之间的高度差不超过17.每座山最多只能改变一次高度,每次改变高度都会产生一定的 ...
- js实现类似于add(1)(2)(3)调用方式的方法
群里有人说实现类似add(1)(2)(3)调用方式的方法,结果马上有人回答: var add = function(a){ return function(b){ return function(c) ...
- Windows应用程序要点
一个完整的Windows应用程序除了WinMain函数外,还包含用于处理用户动作和窗口消息的窗口函数. Windows应用程序具有的一些特性: 消息驱动机制 图形设备接口(GDI) 基于资源的程序设 ...
- JavaScript window.setTimeout() 的详细用法
setTimeout (表达式,延时时间)setTimeout(表达式,交互时间) 延时时间/交互时间是以豪秒为单位的(1000ms=1s) setTimeout 在执行时,是在载入后延迟指定时间后, ...
- Processes and Threads (转)
http://www.cnblogs.com/xitang/archive/2011/09/24/2189460.html 原文 http://developer.android.com/guide/ ...
- BroadcastReceiver的两种注册方式之------动态注册
activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&qu ...
- 安装tomcat过程中出现问题小结
报错信息如下:Neither the JAVA_HOME nor the JRE_HOME environment variable is defined At least one of these ...
- C#对象序列化笔记
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.R ...
- PAT (Advanced Level) 1066. Root of AVL Tree (25)
AVL树的旋转.居然1A了.... 了解旋转方式之后,数据较小可以当做模拟写. #include<cstdio> #include<cstring> #include<c ...
- android异步加载AsyncTask
http://blog.csdn.net/abc5382334/article/details/17097633 http://keeponmoving.iteye.com/blog/1515611 ...