徐凡的blog

欢迎光临,原创不多,转载多多,但都是好东西---------------------有压力就有更多的动力,无压力将有更多的创新

徐凡ID:chief1985
175063次访问,排名419(-1)好友26人,关注者77
好好学习
chief1985的文章
原创 697 篇
翻译 0 篇
转载 293 篇
评论 37 篇
徐的公告
我的得益网加分地址,请多点击
欢迎使用下面的feed订阅:
Dict.CN 在线词典, 英语学习, 在线翻译 Locations of visitors to this page
最近评论
weilmt:.......
immuneiii:谢谢楼主 学习了
wangjing198955:偶到大牛这踩了不知几回了,可惜还是不会建立4*n 多米若骨牌的邻接矩阵,不知大牛可不可以教偶如何建立,偶觉得这个有点难【QQ:445335670】
holiday1001:文章分类涉及面挺广呀。
tdzl2009:指出一个错误:
_DLLMainCRTStartup

WinMainCRTStartup/(MainCRTStartup)
这两个名字在DLL/EXE文件中是可以不存在的,真正决定程序开始位置的是PE文件中一个叫入口点Entry Point的东西,它直接指向二进制代码的某一个位置。
在可执行文件被链接器生成的时候,可以通过选项指定入口点,……
文章分类
收藏
相册
好的网站
80x86汇编小站
I3S汉语词法分析系统ICTCLAS
java学习笔记
Linux那些事儿
Logo在线制作
MSDN每日追踪(RSS)
VCASMO - 一个类似ppt的展示网站
Voiz_经典语句
web前端技术交流博客
windows核心编程(RSS)
中国X黑客小组
中国建站
亲亲我的土豆
仿yahoo
何宗键@BLOG (RSS)
像素精灵
北京大学搜索引擎与互联网信息挖掘组
大学课程在线
孟岩
研学论坛
编程王
网页链接快照
车东
雨林的专栏(RSS)
漂亮的网站
Begin to play with Ext Framework
古典flash
存档
软件项目交易
订阅我的博客
XML聚合  FeedSky

转载 四种排序算法学习收藏

新一篇: 1. 遗传算法的数据结构 | 旧一篇: 算法设计与分析 ppt

导读:   无聊中eMule了一份中文版的<<算法导论>>来看,发现是1993年出版的古物,寒。   在网上google一通,发现一个网站Computer Programming Algorithms Directory,进去先看看Sorting Algorithms部分。   所以今天学习了一下四种排序算法,见Andrew Kitchen的Sorting Algorithms的applet动画显示。   Bubble Sort      /*   * @(#)BubbleSortAlgorithm.java 1.6f 95/01/31 James Gosling   *   * Copyright (c) 1994-1995 Sun Microsystems, Inc. All Rights Reserved.   *   * Permission to use, copy, modify, and distribute this software   * and its documentation for NON-COMMERCIAL or COMMERCIAL purposes and   * without fee is hereby granted.   * Please refer to the file http://java.sun.com/copy_trademarks.html   * for further important copyright and trademark information and to   * http://java.sun.com/licensing.html for further important licensing   * information for the Java (tm) Technology.   *   * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF   * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED   * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A   * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR   * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR   * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.   *   * THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE   * CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE   * PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT   * NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE   * SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE   * SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE   * PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES"). SUN   * SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR   * HIGH RISK ACTIVITIES.   */   /**   * A bubble sort demonstration algorithm   * SortAlgorithm.java, Thu Oct 27 10:32:35 1994   *   * @author James Gosling   * @version 1.6f, 31 Jan 1995   */   class BubbleSortAlgorithm extends SortAlgorithm {   void sort(int a[]) throws Exception {   for (int i = a.length; --i>=0; )   for (int j = 0; j  if (stopRequested) {   return;   }   if (a[j] > a[j+1]) {   int T = a[j];   a[j] = a[j+1];   a[j+1] = T;   }   pause(i,j);   }   pause(-1,-1);   }   }      Bubble Sort is a sequential algorithm, with an average case time of O(n2).   经典的冒泡排序。这种方法的基本思想是,将待排序的元素看作是竖着排列的“气泡”,较小的元素比较轻,从而要往上浮。在冒泡排序算法中我们要对这个“气泡”序列处理   若干遍。所谓一遍处理,就是自底向上检查一遍这个序列,并时刻注意两个相邻的元素的顺序是否正确。如果发现两个相邻元素的顺序不对,即“轻”的元素在下面,就交换它   们的位置。显然,处理一遍之后,“最轻”的元素就浮到了最高位置;处理二遍之后,“次轻”的元素就浮到了次高位置。在作第二遍处理时,由于最高位置上的元素已是“最   轻”元素,所以不必检查。一般地,第i遍处理时,不必检查第i高位置以上的元素,因为经过前面i-1遍的处理,它们已正确地排好序。   以上解释来自http://algorithm.diy.myrice.com/algorithm/commonalg/sort/internal_sorting/bubble_sort/bubble_sort.htm   Quick Sort      /*   * @(#)QSortAlgorithm.java 1.6f 95/01/31 James Gosling   *   * Copyright (c) 1994-1995 Sun Microsystems, Inc. All Rights Reserved.   *   * Permission to use, copy, modify, and distribute this software   * and its documentation for NON-COMMERCIAL or COMMERCIAL purposes and   * without fee is hereby granted.   * Please refer to the file http://java.sun.com/copy_trademarks.html   * for further important copyright and trademark information and to   * http://java.sun.com/licensing.html for further important licensing   * information for the Java (tm) Technology.   *   * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF   * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED   * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A   * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR   * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR   * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.   *   * THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE   * CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE   * PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT   * NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE   * SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE   * SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE   * PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES"). SUN   * SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR   * HIGH RISK ACTIVITIES.   */   /**   * A quick sort demonstration algorithm   * SortAlgorithm.java, Thu Oct 27 10:32:35 1994   *   * @author James Gosling   * @version 1.6f, 31 Jan 1995   */   class QSortAlgorithm extends SortAlgorithm {   void sort(int a[], int lo0, int hi0) throws Exception {   int lo = lo0;   int hi = hi0;   pause(lo, hi);   if (lo >= hi) {   return;   }   int mid = a[(lo + hi) / 2];   while (lo   while (lo  lo++;   }   while (lo  hi--;   }   if (lo   int T = a[lo];   a[lo] = a[hi];   a[hi] = T;   pause();   }   }   if (hi   int T = hi;   hi = lo;   lo = T;   }   sort(a, lo0, lo);   sort(a, lo == lo0 ? lo+1 : lo, hi0);   }   void sort(int a[]) throws Exception {   sort(a, 0, a.length-1);   pause(-1,-1);   }   }      Quick Sort is a sequential algorithm, with an average case time of O(n log n).   快速排序的基本思想是基于分治策略的。对于输入的子序列L[p..r],如果规模足够小则直接进行排序,否则分三步处理:   1,分解(Divide):将输入的序列L[p..r]划分成两个非空子序列L[p..q]和L[q+1..r],使L[p..q]中任一元素的值不大于L[q+1..r]中任一元素的值。   2,递归求解(Conquer):通过递归调用快速排序算法分别对L[p..q]和L[q+1..r]进行排序。   3,合并(Merge):由于对分解出的两个子序列的排序是就地进行的,所以在L[p..q]和L[q+1..r]都排好序后不需要执行任何计算L[p..r]就已排好序。   这个解决流程是符合分治法的基本步骤的。因此,快速排序法是分治法的经典应用实例之一。   以上解释来自http://algorithm.diy.myrice.com/algorithm/commonalg/sort/internal_sorting/quick_sort/quick_sort.htm   Odd-Even Transposition Sort      /*   * @(#)OETransSortAlgorithm.java 95/11/22 Andrew Kitchen   *   */   /**   * An Odd-Even Transposition sort demonstration algorithm   *   * @author Andrew Kitchen   * @version 22 Nov 1995   */   class OETransSortAlgorithm extends SortAlgorithm {   void sort(int a[]) throws Exception {   pause(0,a.length-1);   for (int i = 0; i   if (stopRequested) {   return;   }   for (int j = 0; j+1   if (a[j] > a[j+1]) {   int T = a[j];   a[j] = a[j+1];   a[j+1] = T;   }   pause(); pause();   for (int j = 1; j+1   if (a[j] > a[j+1]) {   int T = a[j];   a[j] = a[j+1];   a[j+1] = T;   }   pause(); pause();   }   pause(-1,-1);   }   }      Odd-Even Transposition Sort is a parallel algorithm, with an worst case time of O(n), running on n processors. Its absolute speed up   is O(log n), so its efficiency is O((log n)/n).   主要思想为奇偶位两个循环并行排序。   Shear Sort      /**   * A shear sort demonstration algorithm   * SortAlgorithm.java, Thu Nov 27 1995   *   * author Andrew Kitchen   */   class ShearSortAlgorithm extends SortAlgorithm {   private int Log, Rows, Cols;   void sort(int a[]) throws Exception {   int pow=1, div=1;   int h[];   for(int i=1; i*i<=a.length; i++) <br >  if (a.length % i == 0) div = i;   Rows = div; Cols = a.length / div;   for(Log=0; pow<=Rows; Log++)   pow = pow * 2;   h = new int[Rows];   for (int i=0; i  h[i]=i*Cols;   for (int k=0; k  for (int j=0; j  for (int i=0; i  sortPart1(a,i*Cols,(i+1)*Cols,1,(i%2==0?true:false));   apause(h);   for (int i=0; i  sortPart2(a,i*Cols,(i+1)*Cols,1,(i%2==0?true:false));   apause(h);   }   for (int j=0; j  for (int i=0; i  sortPart1(a,i,Rows*Cols+i,Cols,true);   apause(h);   for (int i=0; i  sortPart2(a,i,Rows*Cols+i,Cols,true);   apause(h);   }   }   for (int j=0; j  for (int i=0; i  sortPart1(a,i*Cols,(i+1)*Cols,1,true);   apause(h);   for (int i=0; i  sortPart2(a,i*Cols,(i+1)*Cols,1,true);   apause(h);   }   for (int i=0; i  h[i]=-1;   apause(h);   }   private void sortPart1(int a[], int Lo, int Hi, int Nx, boolean Up) throws Exception {   for (int j = Lo; j+Nx  if ((Up &&a[j] > a[j+Nx]) || !Up &&a[j]   int T = a[j];   a[j] = a[j+Nx];   a[j+Nx] = T;   }   }   private void sortPart2(int a[], int Lo, int Hi, int Nx, boolean Up) throws Exception {   for (int j = Lo+Nx; j+Nx  if ((Up &&a[j] > a[j+Nx]) || !Up &&a[j]   int T = a[j];   a[j] = a[j+Nx];   a[j+Nx] = T;   }   }   }      Shear Sort is a parallel algorithm, with an worst case time of O(n1/2 log n), running on n processors. Its absolute speed up is   O(n1/2), so its efficiency is O(1/n1/2).   主要思想为将N个数的数组分配到长度为sqrt(N)的grid,然后分别对行和列并行排序。 本文转自 http://hideto.javaeye.com/blog/66718

发表于 @ 2008年06月19日 21:30:31|评论(loading...)|收藏

新一篇: 1. 遗传算法的数据结构 | 旧一篇: 算法设计与分析 ppt

评论:没有评论。

发表评论  


当前用户设置只有注册用户才能发表评论。如果你没有登录,请点击登录
Csdn Blog version 3.1a
Copyright © 徐