- 浏览: 440859 次
- 性别:
- 来自: 杭州
文章分类
- 全部博客 (162)
- easymock (3)
- 模板引擎 (3)
- JForum (4)
- web (9)
- spring (10)
- java (20)
- struts (9)
- uml (3)
- java pattern (19)
- JQuery (14)
- 多线程 (13)
- database (21)
- PS (3)
- ejb (6)
- 版本管理 svn , maven , ant (2)
- protocol (1)
- 测试 (1)
- ws (7)
- Apache (4)
- 脚本语言 (1)
- guice (1)
- 分布式 (4)
- 架构 (0)
- 经验 (1)
- 版本管理 svn (1)
- maven (1)
- ant (1)
- 书籍 (1)
- Linux (1)
最新评论
-
Master-Gao:
稍微明白了点,,有点萌萌哒
为什么匿名内部类参数必须为final类型 -
waw0931:
终于明白了,谢谢!
为什么匿名内部类参数必须为final类型 -
十三圆桌骑士:
提供了两个链接还是有用的。
安装Mondrian -
放方芳:
[flash=200,200][/flash]
Freemarker标签使用 -
放方芳:
[b][/b]
Freemarker标签使用
* fast: it is guaranteed to run in n log(n) time andruns substantially faster on nearly sorted lists. empirical tests showed it tobe as fast as a highly optimized quicksort. a quicksort is generally consideredto be faster than a merge sort but isn't stable and doesn't guarantee n log(n)performance.
* stable: it doesn't reorder equal elements. this isimportant if you sort the same list repeatedly on different attributes. if auser of a mail program sorts the inbox by mailing date and then sorts it bysender, the user naturally expects that the now-contiguous list of messagesfrom a given sender will (still) be sorted by mailing date. this is guaranteedonly if the second sort was stable.
也就是说,优化的归并排序既快速(nlog(n))又稳定。
对于对象的排序,稳定性很重要。比如成绩单,一开始可能是按人员的学号顺序排好了的,现在让我们用成绩排,那么你应该保证,本来张三在李四前面,即使他们成绩相同,张三不能跑到李四的后面去。
而快速排序是不稳定的,而且最坏情况下的时间复杂度是o(n^2)。
另外,对象数组中保存的只是对象的引用,这样多次移位并不会造成额外的开销,但是,对象数组对比较次数一般比较敏感,有可能对象的比较比单纯数的比较开销大很多。归并排序在这方面比快速排序做得更好,这也是选择它作为对象排序的一个重要原因之一。
排序优化:实现中快排和归并都采用递归方式,而在递归的底层,也就是待排序的数组长度小于7时, 直接使用冒泡排序,而不再递归下去。
分析:长度为6的数组冒泡排序总比较次数最多也就1+2+3+4+5+6=21次,最好情况下只有6次比较。而快排或归并涉及到递归调用等的开销,其时间效率在n较小时劣势就凸显了,因此这里采用了冒泡排序,这也是对快速排序极重要的优化。
/*快速排序*/
private static void sort1(int x[], int off, int len) {
// insertion sort on smallest arrays
if (len < 7) {
for (int i=off; i<len+off; i++)
for (int j=i; j>off && x[j-1]>x[j]; j--)
swap(x, j, j-1);
return;
}
// choose a partition element, v
int m = off + (len >> 1); // small arrays, middle element
if (len > 7) {
int l = off;
int n = off + len - 1;
if (len > 40) { // big arrays, pseudomedian of 9
int s = len/8;
l = med3(x, l, l+s, l+2*s);//取后三个参数中的中间值
m = med3(x, m-s, m, m+s);
n = med3(x, n-2*s, n-s, n);
}
m = med3(x, l, m, n); // mid-size, med of 3
}
int v = x[m];
// establish invariant: v* (<v)* (>v)* v*
int a = off, b = a, c = off + len - 1, d = c;
while(true) {
while (b <= c && x[b] <= v) {
if (x[b] == v)
swap(x, a++, b);
b++;
}
while (c >= b && x[c] >= v) {
if (x[c] == v)
swap(x, c, d--);
c--;
}
if (b > c)
break;
swap(x, b++, c--);
}
// swap partition elements back to middle
int s, n = off + len;
s = math.min(a-off, b-a ); vecswap(x, off, b-s, s);
s = math.min(d-c, n-d-1); vecswap(x, b, n-s, s);
// recursively sort non-partition-elements
if ((s = b-a) > 1)
sort1(x, off, s);
if ((s = d-c) > 1)
sort1(x, n-s, s);
}
/*归并排序*/
private static void mergesort(object[] src,
object[] dest,
int low,
int high,
int off) {
int length = high - low;
// insertion sort on smallest arrays
if (length < insertionsort_threshold) {
for (int i=low; i<high; i++)
for (int j=i; j>low &&
((comparable) dest[j-1]).compareto(dest[j])>0; j--)
swap(dest, j, j-1);
return;
}
// recursively sort halves of dest into src
int destlow = low;
int desthigh = high;
low += off;
high += off;
int mid = (low + high) >>> 1;
mergesort(dest, src, low, mid, -off);
mergesort(dest, src, mid, high, -off);
// if list is already sorted, just copy from src to dest. this is an
// optimization that results in faster sorts for nearly ordered lists.
if (((comparable)src[mid-1]).compareto(src[mid]) <= 0) {
system.arraycopy(src, low, dest, destlow, length);
return;
}
// merge sorted halves (now in src) into dest
for(int i = destlow, p = low, q = mid; i < desthigh; i++) {
if (q >= high || p < mid && ((comparable)src[p]).compareto(src[q])<=0)
dest[i] = src[p++];
else
dest[i] = src[q++];
}
}
* stable: it doesn't reorder equal elements. this isimportant if you sort the same list repeatedly on different attributes. if auser of a mail program sorts the inbox by mailing date and then sorts it bysender, the user naturally expects that the now-contiguous list of messagesfrom a given sender will (still) be sorted by mailing date. this is guaranteedonly if the second sort was stable.
也就是说,优化的归并排序既快速(nlog(n))又稳定。
对于对象的排序,稳定性很重要。比如成绩单,一开始可能是按人员的学号顺序排好了的,现在让我们用成绩排,那么你应该保证,本来张三在李四前面,即使他们成绩相同,张三不能跑到李四的后面去。
而快速排序是不稳定的,而且最坏情况下的时间复杂度是o(n^2)。
另外,对象数组中保存的只是对象的引用,这样多次移位并不会造成额外的开销,但是,对象数组对比较次数一般比较敏感,有可能对象的比较比单纯数的比较开销大很多。归并排序在这方面比快速排序做得更好,这也是选择它作为对象排序的一个重要原因之一。
排序优化:实现中快排和归并都采用递归方式,而在递归的底层,也就是待排序的数组长度小于7时, 直接使用冒泡排序,而不再递归下去。
分析:长度为6的数组冒泡排序总比较次数最多也就1+2+3+4+5+6=21次,最好情况下只有6次比较。而快排或归并涉及到递归调用等的开销,其时间效率在n较小时劣势就凸显了,因此这里采用了冒泡排序,这也是对快速排序极重要的优化。
/*快速排序*/
private static void sort1(int x[], int off, int len) {
// insertion sort on smallest arrays
if (len < 7) {
for (int i=off; i<len+off; i++)
for (int j=i; j>off && x[j-1]>x[j]; j--)
swap(x, j, j-1);
return;
}
// choose a partition element, v
int m = off + (len >> 1); // small arrays, middle element
if (len > 7) {
int l = off;
int n = off + len - 1;
if (len > 40) { // big arrays, pseudomedian of 9
int s = len/8;
l = med3(x, l, l+s, l+2*s);//取后三个参数中的中间值
m = med3(x, m-s, m, m+s);
n = med3(x, n-2*s, n-s, n);
}
m = med3(x, l, m, n); // mid-size, med of 3
}
int v = x[m];
// establish invariant: v* (<v)* (>v)* v*
int a = off, b = a, c = off + len - 1, d = c;
while(true) {
while (b <= c && x[b] <= v) {
if (x[b] == v)
swap(x, a++, b);
b++;
}
while (c >= b && x[c] >= v) {
if (x[c] == v)
swap(x, c, d--);
c--;
}
if (b > c)
break;
swap(x, b++, c--);
}
// swap partition elements back to middle
int s, n = off + len;
s = math.min(a-off, b-a ); vecswap(x, off, b-s, s);
s = math.min(d-c, n-d-1); vecswap(x, b, n-s, s);
// recursively sort non-partition-elements
if ((s = b-a) > 1)
sort1(x, off, s);
if ((s = d-c) > 1)
sort1(x, n-s, s);
}
/*归并排序*/
private static void mergesort(object[] src,
object[] dest,
int low,
int high,
int off) {
int length = high - low;
// insertion sort on smallest arrays
if (length < insertionsort_threshold) {
for (int i=low; i<high; i++)
for (int j=i; j>low &&
((comparable) dest[j-1]).compareto(dest[j])>0; j--)
swap(dest, j, j-1);
return;
}
// recursively sort halves of dest into src
int destlow = low;
int desthigh = high;
low += off;
high += off;
int mid = (low + high) >>> 1;
mergesort(dest, src, low, mid, -off);
mergesort(dest, src, mid, high, -off);
// if list is already sorted, just copy from src to dest. this is an
// optimization that results in faster sorts for nearly ordered lists.
if (((comparable)src[mid-1]).compareto(src[mid]) <= 0) {
system.arraycopy(src, low, dest, destlow, length);
return;
}
// merge sorted halves (now in src) into dest
for(int i = destlow, p = low, q = mid; i < desthigh; i++) {
if (q >= high || p < mid && ((comparable)src[p]).compareto(src[q])<=0)
dest[i] = src[p++];
else
dest[i] = src[q++];
}
}
发表评论
-
swingworker
2015-07-15 14:18 0Swing应用程序员常见的错误是误用Swin ... -
理解Java对象序列化(转)
2014-05-14 22:29 0理解Java对象序列化 关于Java序列化的文章早已是汗牛 ... -
web项目中Log4j不输出到文件问题
2013-03-06 17:50 17755log4j.propert ... -
java 补码解释
2012-07-07 10:46 8461.byte的大小为8bits而int的大小为32bit ... -
jms基础概念和应用场景(转)
2012-06-13 13:34 1130原文地址:http://blog.csdn.net/KimmK ... -
java GC和运行参数
2012-05-07 21:27 0java GC和运行参数 引言有JAVA开 ... -
java类加载顺序
2012-04-26 18:58 1398当程序中调用 new 指令,或 ... -
callable和future
2012-04-26 18:29 1052import java.util.concurren ... -
为什么匿名内部类参数必须为final类型
2012-04-26 18:26 285631) 从程序设计语言的理论上:局部内部类(即:定义在方法中的 ... -
JAVA 使用final参数的原因
2012-04-06 14:59 2326先介绍一些基本概念。 ... -
Java 强引用、 软引用、 弱引用、虚引用
2012-03-06 15:23 8311 .对象的强、软、 ... -
java内存
2012-03-05 15:12 0堆大小设置 JVM 中最大堆大小有三方 ... -
Java泛型详解
2011-11-23 17:49 928优点概述:是对 Java 语言的类型系统的一种扩展,规定集合中 ... -
java 堆和栈
2011-11-08 17:44 784简单概括一下: java栈 存放 基本类型的字面 ... -
jvm全局理解
2011-09-11 15:54 10591 Java技术与Java虚拟机 说起Java,人们 ... -
hashmap死循环
2011-08-25 22:29 2313本文受http://pt.alibaba-inc. ... -
hashmap
2011-08-24 21:29 1122在Java中任何一个对象都具备equals(Object ... -
properties 占位符
2011-05-16 15:09 2177MessageFormat - java.text.M ... -
EditPlus正则表达式替换字符串详解
2010-10-12 15:51 1219正则表达式是一个查询 ... -
HashMap 源码解读
2010-08-05 16:56 1384HashMap是我们在日常写代码时最常用到的一个数据结构,它为 ...
相关推荐
JavaScript的Array对象有一个sort方法,用于实现对数组元素的排序,该方法默认按照数组项ASCII 字符顺序升序排列。 如[6,7,9,1,-1].sort();执行后数组变为[-1,1,6,7,9]。 对于需要降序排列或非字符串排序,该...
js代码-sort源码实现原理
c#中的Collections Data Structure用法array sort。是一些源码,txt格式的。
JavaScript中数组的sort()方法主要用于对数组的元素进行排序。其中,sort()方法有一个可选参数。但是,此参数必须是函数。 数组在调用sort()方法时,如果没有传参将按字母顺序(字符编码顺序)对数组中的元素进行...
java中的Array.sort()源码分析 明日任务: 进一步分析Array.sort() 二分查找学习 今日总结:Array.sort()阅读有多次放弃想法,最终还是自己啃下来了,耐力还是要都锻炼,一定要坚持~ day38 今日任务: 桶排序、计数...
Array Array 类、Array() asfunction asfunction asin Math.asin() atan Math.atan() atan2 Math.atan2() attachAudio MovieClip.attachAudio() attachMovie MovieClip.attachMovie() attachSound ...
语法:arrayObject.sort(sortby);参数sortby可选。规定排序顺序。必须是函数。 sort() 方法用于对数组的元素进行排序。 如果调用该方法时没有使用参数,将按字母顺序对数组中的元素进行排序,说得更精确点,是按照...
Column 8: Compute the maximum-sum subsequence in an array maxsum.c -- Time four algs: n3, n2, n log n, n. Column 9: Code tuning programs genbins.c -- Profile this, then try a special-purpose ...
records array, ignoring local filter of the DataSet. TDBGridEh uses this property for automatic filling a list in DropDownBox of the subtitle filter cell. TDataDriverEh component carry out two ...
一个排序可以用的C 函数模板,无意间需要对字符串集合CStringArray进行排序,但标准模板库STL提供的函数模板sort虽然功能强大,不过有些不便,可能我不太习惯吧,于是才想着要自编一个排序函数模板,方便和我一样对C...
这个方法是我见过对高效的。 代码如下: var arr=[];...//而不推荐使用 var arr=new Array(); 这句不用解释了。 for(var i=0;i<100;i++){ arr[i]=i; }//循环给数组赋值 关键第地方来了 代码如下: 代码 arr.sort
飞翔的小鸟java源码力码解决方案 1 /** * @param {number[]} nums * @return {number[]} */ var sortArray = function(nums) { if(nums.length <= 1){ return nums} //splitarray let a = sortArray(nums.slice(0...
1.13.3 变量的重解释 1.14 根据条件进行决策 1.14.1 if语句 1.14.2 代码块 1.14.3 else语句 1.14.4 elseif语句 1.14.5 switch语句 1.14.6 比较不同的条件 1.15 通过迭代实现重复动作 1.15.1 while循环 ...
array.zip A simple program that shows how a two-dimensional array works within a VB program.<END><br>70,Bubblesort.zip A simple Bubble Sort code that shows how the program works within a VB ...
The grid was formed with array of text boxes.<END><br>33,txtgrid.zip This program creates text box array dynamically and loads the database fields onto it. <END><br>34,DAOLike.zip This program ...
(4KB)<END><br>99,tokeniterator.zip Token Iterator provides an easy to use, familiar, and customizable way in which to go through the tokens contained in a string (7KB)<END><br>100,stdsort.zip ...
集合原始java 工厂 下面给出的.pdf文件中的代码距离OO还很短。 OhNo_NoOO.pdf您的工作是将代码重构为适当的层次结构。 利用“依赖倒置”来查找各项之间的共性并构建适当的...为了使Collections.sort()起作用,Array
C# 常见算法案例源码,C# 算法学习 面试以及后续工作算法学习 Array BasicTest BitOperation Collections DataStructureAndAlgorithm DesignPattern DynamicProgramming Heap HighQuality LinkedList Matrix ...
实现了一些比较常见的array功能,并且使用了 jest 进行测试。 实现的方法包括: concat find flat forEach & map includes join pop push reduce reverse shift slice some sort splice unshift 有些同质化比较高的...
│ │ TopologicalSort.cpp │ │ TopologicalSort.h │ │ VertexType.cpp │ │ VertexType.h │ │ │ ├─插入排序 │ │ 1.txt │ │ main.cpp │ │ RedType.cpp │ │ RedType.h │ │ Sq_InsertSort.cpp │ ...