1. package org.rut.util.algorithm.support;
  2.  
  3. import org.rut.util.algorithm.SortUtil;
  4. /**
  5. * @author treeroot
  6. * @since 2006-2-2
  7. * @version 1.0
  8. */
  9. public class InsertSort implements SortUtil.Sort{
  10.  
  11. /** (non-Javadoc)
  12. * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
  13. */
  14. public void sort(int[] data) {
  15. int temp;
  16. for(int i=1;i<data.length;i++){
  17. for(int j=i;(j>0)&&(data[j]<data[j-1]);j--){
  18. SortUtil.swap(data,j,j-1);
  19. }
  20. }
  21. }
  22.  
  23. }
  24. //冒泡排序:
  25.  
  26. for(var i=0; i<arr.length; i++) {
  27. for(var j=i+1; j<=arr.length-1; j++) {
  28. if(eval(arr[i]) < eval(arr[j])) {
  29. temp = arr[i];
  30. arr[i] = arr[j];
  31. arr[j] = temp;
  32. }
  33. }
  34. }
  35.  
  36. package org.rut.util.algorithm.support;
  37.  
  38. import org.rut.util.algorithm.SortUtil;
  39.  
  40. /**
  41. * @author treeroot
  42. * @since 2006-2-2
  43. * @version 1.0
  44. */
  45. public class BubbleSort implements SortUtil.Sort{
  46.  
  47. /** (non-Javadoc)
  48. * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
  49. */
  50. public void sort(int[] data) {
  51. int temp;
  52. for(int i=0;i<data.length;i++){
  53. for(int j=data.length-1;j>i;j--){
  54. if(data[j]<data[j-1]){
  55. SortUtil.swap(data,j,j-1);
  56. }
  57. }
  58. }
  59. }
  60.  
  61. }
  62.  
  63. //选择排序:
  64.  
  65. package org.rut.util.algorithm.support;
  66.  
  67. import org.rut.util.algorithm.SortUtil;
  68.  
  69. /**
  70. * @author treeroot
  71. * @since 2006-2-2
  72. * @version 1.0
  73. */
  74. public class SelectionSort implements SortUtil.Sort {
  75.  
  76. /**
  77. * (non-Javadoc)
  78. *
  79. * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
  80. */
  81. public void sort(int[] data) {
  82. int temp;
  83. for (int i = 0; i < data.length; i++) {
  84. int lowIndex = i;
  85. for (int j = data.length - 1; j > i; j--) {
  86. if (data[j] < data[lowIndex]) {
  87. lowIndex = j;
  88. }
  89. }
  90. SortUtil.swap(data,i,lowIndex);
  91. }
  92. }
  93.  
  94. }
  95.  
  96. //Shell排序:
  97.  
  98. package org.rut.util.algorithm.support;
  99.  
  100. import org.rut.util.algorithm.SortUtil;
  101.  
  102. /**
  103. * @author treeroot
  104. * @since 2006-2-2
  105. * @version 1.0
  106. */
  107. public class ShellSort implements SortUtil.Sort{
  108.  
  109. /** (non-Javadoc)
  110. * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
  111. */
  112. public void sort(int[] data) {
  113. for(int i=data.length/2;i>2;i/=2){
  114. for(int j=0;j<i;j++){
  115. insertSort(data,j,i);
  116. }
  117. }
  118. insertSort(data,0,1);
  119. }
  120.  
  121. /**
  122. * @param data
  123. * @param j
  124. * @param i
  125. */
  126. private void insertSort(int[] data, int start, int inc) {
  127. int temp;
  128. for(int i=start+inc;i<data.length;i+=inc){
  129. for(int j=i;(j>=inc)&&(data[j]<data[j-inc]);j-=inc){
  130. SortUtil.swap(data,j,j-inc);
  131. }
  132. }
  133. }
  134.  
  135. }
  136.  
  137. //快速排序:
  138.  
  139. package org.rut.util.algorithm.support;
  140.  
  141. import org.rut.util.algorithm.SortUtil;
  142.  
  143. /**
  144. * @author treeroot
  145. * @since 2006-2-2
  146. * @version 1.0
  147. */
  148. public class QuickSort implements SortUtil.Sort{
  149.  
  150. /** (non-Javadoc)
  151. * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
  152. */
  153. public void sort(int[] data) {
  154. quickSort(data,0,data.length-1);
  155. }
  156. private void quickSort(int[] data,int i,int j){
  157. int pivotIndex=(i+j)/2;
  158. //swap
  159. SortUtil.swap(data,pivotIndex,j);
  160.  
  161. int k=partition(data,i-1,j,data[j]);
  162. SortUtil.swap(data,k,j);
  163. if((k-i)>1) quickSort(data,i,k-1);
  164. if((j-k)>1) quickSort(data,k+1,j);
  165.  
  166. }
  167. /**
  168. * @param data
  169. * @param i
  170. * @param j
  171. * @return
  172. */
  173. private int partition(int[] data, int l, int r,int pivot) {
  174. do{
  175. while(data[++l]<pivot);
  176. while((r!=0)&&data[--r]>pivot);
  177. SortUtil.swap(data,l,r);
  178. }
  179. while(l<r);
  180. SortUtil.swap(data,l,r);
  181. return l;
  182. }
  183.  
  184. }
  185. //改进后的快速排序:
  186.  
  187. package org.rut.util.algorithm.support;
  188.  
  189. import org.rut.util.algorithm.SortUtil;
  190.  
  191. /**
  192. * @author treeroot
  193. * @since 2006-2-2
  194. * @version 1.0
  195. */
  196. public class ImprovedQuickSort implements SortUtil.Sort {
  197.  
  198. private static int MAX_STACK_SIZE=4096;
  199. private static int THRESHOLD=10;
  200. /** (non-Javadoc)
  201. * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
  202. */
  203. public void sort(int[] data) {
  204. int[] stack=new int[MAX_STACK_SIZE];
  205.  
  206. int top=-1;
  207. int pivot;
  208. int pivotIndex,l,r;
  209.  
  210. stack[++top]=0;
  211. stack[++top]=data.length-1;
  212.  
  213. while(top>0){
  214. int j=stack[top--];
  215. int i=stack[top--];
  216.  
  217. pivotIndex=(i+j)/2;
  218. pivot=data[pivotIndex];
  219.  
  220. SortUtil.swap(data,pivotIndex,j);
  221.  
  222. //partition
  223. l=i-1;
  224. r=j;
  225. do{
  226. while(data[++l]<pivot);
  227. while((r!=0)&&(data[--r]>pivot));
  228. SortUtil.swap(data,l,r);
  229. }
  230. while(l<r);
  231. SortUtil.swap(data,l,r);
  232. SortUtil.swap(data,l,j);
  233.  
  234. if((l-i)>THRESHOLD){
  235. stack[++top]=i;
  236. stack[++top]=l-1;
  237. }
  238. if((j-l)>THRESHOLD){
  239. stack[++top]=l+1;
  240. stack[++top]=j;
  241. }
  242.  
  243. }
  244. //new InsertSort().sort(data);
  245. insertSort(data);
  246. }
  247. /**
  248. * @param data
  249. */
  250. private void insertSort(int[] data) {
  251. int temp;
  252. for(int i=1;i<data.length;i++){
  253. for(int j=i;(j>0)&&(data[j]<data[j-1]);j--){
  254. SortUtil.swap(data,j,j-1);
  255. }
  256. }
  257. }
  258.  
  259. }
  260.  
  261. //归并排序:
  262.  
  263. package org.rut.util.algorithm.support;
  264.  
  265. import org.rut.util.algorithm.SortUtil;
  266.  
  267. /**
  268. * @author treeroot
  269. * @since 2006-2-2
  270. * @version 1.0
  271. */
  272. public class MergeSort implements SortUtil.Sort{
  273.  
  274. /** (non-Javadoc)
  275. * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
  276. */
  277. public void sort(int[] data) {
  278. int[] temp=new int[data.length];
  279. mergeSort(data,temp,0,data.length-1);
  280. }
  281.  
  282. private void mergeSort(int[] data,int[] temp,int l,int r){
  283. int mid=(l+r)/2;
  284. if(l==r) return ;
  285. mergeSort(data,temp,l,mid);
  286. mergeSort(data,temp,mid+1,r);
  287. for(int i=l;i<=r;i++){
  288. temp[i]=data[i];
  289. }
  290. int i1=l;
  291. int i2=mid+1;
  292. for(int cur=l;cur<=r;cur++){
  293. if(i1==mid+1)
  294. data[cur]=temp[i2++];
  295. else if(i2>r)
  296. data[cur]=temp[i1++];
  297. else if(temp[i1]<temp[i2])
  298. data[cur]=temp[i1++];
  299. else
  300. data[cur]=temp[i2++];
  301. }
  302. }
  303.  
  304. }
  305.  
  306. //改进后的归并排序:
  307.  
  308. package org.rut.util.algorithm.support;
  309.  
  310. import org.rut.util.algorithm.SortUtil;
  311.  
  312. /**
  313. * @author treeroot
  314. * @since 2006-2-2
  315. * @version 1.0
  316. */
  317. public class ImprovedMergeSort implements SortUtil.Sort {
  318.  
  319. private static final int THRESHOLD = 10;
  320.  
  321. /**
  322. * (non-Javadoc)
  323. *
  324. * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
  325. */
  326. public void sort(int[] data) {
  327. int[] temp=new int[data.length];
  328. mergeSort(data,temp,0,data.length-1);
  329. }
  330.  
  331. private void mergeSort(int[] data, int[] temp, int l, int r) {
  332. int i, j, k;
  333. int mid = (l + r) / 2;
  334. if (l == r)
  335. return;
  336. if ((mid - l) >= THRESHOLD)
  337. mergeSort(data, temp, l, mid);
  338. else
  339. insertSort(data, l, mid - l + 1);
  340. if ((r - mid) > THRESHOLD)
  341. mergeSort(data, temp, mid + 1, r);
  342. else
  343. insertSort(data, mid + 1, r - mid);
  344.  
  345. for (i = l; i <= mid; i++) {
  346. temp[i] = data[i];
  347. }
  348. for (j = 1; j <= r - mid; j++) {
  349. temp[r - j + 1] = data[j + mid];
  350. }
  351. int a = temp[l];
  352. int b = temp[r];
  353. for (i = l, j = r, k = l; k <= r; k++) {
  354. if (a < b) {
  355. data[k] = temp[i++];
  356. a = temp[i];
  357. } else {
  358. data[k] = temp[j--];
  359. b = temp[j];
  360. }
  361. }
  362. }
  363.  
  364. /**
  365. * @param data
  366. * @param l
  367. * @param i
  368. */
  369. private void insertSort(int[] data, int start, int len) {
  370. for(int i=start+1;i<start+len;i++){
  371. for(int j=i;(j>start) && data[j]<data[j-1];j--){
  372. SortUtil.swap(data,j,j-1);
  373. }
  374. }
  375. }
  376.  
  377. }
  378. //堆排序:
  379.  
  380. package org.rut.util.algorithm.support;
  381.  
  382. import org.rut.util.algorithm.SortUtil;
  383.  
  384. /**
  385. * @author treeroot
  386. * @since 2006-2-2
  387. * @version 1.0
  388. */
  389. public class HeapSort implements SortUtil.Sort{
  390.  
  391. /** (non-Javadoc)
  392. * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
  393. */
  394. public void sort(int[] data) {
  395. MaxHeap h=new MaxHeap();
  396. h.init(data);
  397. for(int i=0;i<data.length;i++)
  398. h.remove();
  399. System.arraycopy(h.queue,1,data,0,data.length);
  400. }
  401.  
  402. private static class MaxHeap{
  403.  
  404. void init(int[] data){
  405. this.queue=new int[data.length+1];
  406. for(int i=0;i<data.length;i++){
  407. queue[++size]=data[i];
  408. fixUp(size);
  409. }
  410. }
  411.  
  412. private int size=0;
  413.  
  414. private int[] queue;
  415.  
  416. public int get() {
  417. return queue[1];
  418. }
  419.  
  420. public void remove() {
  421. SortUtil.swap(queue,1,size--);
  422. fixDown(1);
  423. }
  424. //fixdown
  425. private void fixDown(int k) {
  426. int j;
  427. while ((j = k << 1) <= size) {
  428. if (j < size && queue[j]<queue[j+1])
  429. j++;
  430. if (queue[k]>queue[j]) //不用交换
  431. break;
  432. SortUtil.swap(queue,j,k);
  433. k = j;
  434. }
  435. }
  436. private void fixUp(int k) {
  437. while (k > 1) {
  438. int j = k >> 1;
  439. if (queue[j]>queue[k])
  440. break;
  441. SortUtil.swap(queue,j,k);
  442. k = j;
  443. }
  444. }
  445.  
  446. }
  447.  
  448. }
  449.  
  450. //SortUtil:
  451.  
  452. package org.rut.util.algorithm;
  453.  
  454. import org.rut.util.algorithm.support.BubbleSort;
  455. import org.rut.util.algorithm.support.HeapSort;
  456. import org.rut.util.algorithm.support.ImprovedMergeSort;
  457. import org.rut.util.algorithm.support.ImprovedQuickSort;
  458. import org.rut.util.algorithm.support.InsertSort;
  459. import org.rut.util.algorithm.support.MergeSort;
  460. import org.rut.util.algorithm.support.QuickSort;
  461. import org.rut.util.algorithm.support.SelectionSort;
  462. import org.rut.util.algorithm.support.ShellSort;
  463.  
  464. /**
  465. * @author treeroot
  466. * @since 2006-2-2
  467. * @version 1.0
  468. */
  469. public class SortUtil {
  470. public final static int INSERT = 1;
  471.  
  472. public final static int BUBBLE = 2;
  473.  
  474. public final static int SELECTION = 3;
  475.  
  476. public final static int SHELL = 4;
  477.  
  478. public final static int QUICK = 5;
  479.  
  480. public final static int IMPROVED_QUICK = 6;
  481.  
  482. public final static int MERGE = 7;
  483.  
  484. public final static int IMPROVED_MERGE = 8;
  485.  
  486. public final static int HEAP = 9;
  487.  
  488. public static void sort(int[] data) {
  489. sort(data, IMPROVED_QUICK);
  490. }
  491. private static String[] name={
  492. "insert","bubble","selection","shell","quick","improved_quick","merge","improved_merge","heap"
  493. };
  494.  
  495. private static Sort[] impl=new Sort[]{
  496. new InsertSort(),
  497. new BubbleSort(),
  498. new SelectionSort(),
  499. new ShellSort(),
  500. new QuickSort(),
  501. new ImprovedQuickSort(),
  502. new MergeSort(),
  503. new ImprovedMergeSort(),
  504. new HeapSort()
  505. };
  506.  
  507. public static String toString(int algorithm){
  508. return name[algorithm-1];
  509. }
  510.  
  511. public static void sort(int[] data, int algorithm) {
  512. impl[algorithm-1].sort(data);
  513. }
  514.  
  515. public static interface Sort {
  516. public void sort(int[] data);
  517. }
  518.  
  519. public static void swap(int[] data, int i, int j) {
  520. int temp = data[i];
  521. data[i] = data[j];
  522. data[j] = temp;
  523. }
  524. }

Java中各种排序算法的更多相关文章

  1. Java中的排序算法(2)

    Java中的排序算法(2) * 快速排序 * 快速排序使用分治法(Divide and conquer)策略来把一个序列(list)分为两个子序列(sub-lists). * 步骤为: * 1. 从数 ...

  2. Java 中常见排序算法

    经典的排序算法总结 冒泡排序算法 算法描述: 比较相邻的元素:如果第一个比第二个大,就交换它们两个: 对每一对相邻元素作同样的工作,从开始第一对到结尾的最后一对,这样在最后的元素应该会是最大的数: 针 ...

  3. Java中的经典算法之选择排序(SelectionSort)

    Java中的经典算法之选择排序(SelectionSort) 神话丿小王子的博客主页 a) 原理:每一趟从待排序的记录中选出最小的元素,顺序放在已排好序的序列最后,直到全部记录排序完毕.也就是:每一趟 ...

  4. Java中的经典算法之冒泡排序(Bubble Sort)

    Java中的经典算法之冒泡排序(Bubble Sort) 神话丿小王子的博客主页 原理:比较两个相邻的元素,将值大的元素交换至右端. 思路:依次比较相邻的两个数,将小数放在前面,大数放在后面.即在第一 ...

  5. Java中的查找算法之顺序查找(Sequential Search)

    Java中的查找算法之顺序查找(Sequential Search) 神话丿小王子的博客主页 a) 原理:顺序查找就是按顺序从头到尾依次往下查找,找到数据,则提前结束查找,找不到便一直查找下去,直到数 ...

  6. Comparable与Comparator,java中的排序与比较

    1:比较和排序的概念 比较:两个实体类之间按>,=,<进行比较. 排序:在集合类中,对集合类中的实体进行排序.排序基于的算法基于实体类提供的比较函数. 基本型别都提供了默认的比较算法,如s ...

  7. Java中的经典算法之快速排序(Quick Sort)

    Java中的经典算法之快速排序(Quick Sort) 快速排序的思想 基本思想是:通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小, 然后再按此方法对 ...

  8. STL笔记(6)标准库:标准库中的排序算法

    STL笔记(6)标准库:标准库中的排序算法 标准库:标准库中的排序算法The Standard Librarian: Sorting in the Standard Library Matthew A ...

  9. java实现各种排序算法

    java实现各种排序算法 import java.util.Arrays; public class SomeSort { public static void main(String[] args) ...

随机推荐

  1. [Gauss]POJ1222 EXTENDED LIGHTS OUT

    题意:给一个5*6的矩阵 1代表该位置的灯亮着, 0代表该位置的灯没亮 按某个位置的开关,可以同时改变 该位置 以及 该位置上方.下方.左方.右方, 共五个位置的灯的开.关(1->0, 0-&g ...

  2. HDOJ多校联合第四场

    B题: C题:仅由'A','G','C','T',4个字母组成,给定一个字符串S,|S|<=15,给定一个整数m,以m为长度且仅含4种字母的字符串T,求LCS(S,T)为0,1,2,3....| ...

  3. Java二维数组

    package com.test; public class Test { public static void main(String[] args) { // TODO Auto-generate ...

  4. MySQL 性能调优的10个方法

    Mysql的优化方面,一般我们很少去考虑它,即使想到优化一般也更多是程序级别的,比如不要写过于消耗资源的SQL语句,但是除此以外,在整个系统上其实仍然有很多可以优化的地方. 1. 选择合适的存储引擎: ...

  5. 坑爹的libxml2 for mingw 编译 (二)

    makefile 中由于大量使用了cmd /C ""样式去执行mkdir和copy操作,导致mingw最后出错,因为会从mingw切换至cmd界面.因此需要把相关代码进行修改. # ...

  6. poj3140Contestants Division

    链接 这叫树形DP吗..?断开某条边 求剩下两颗树数权值和的差最小 dfs一遍 枚举边 查了n久 wa n次  dp数组没初始化.. 在poj上1A感觉应该挺爽 #include <iostre ...

  7. [Hadoop源码解读](三)MapReduce篇之Job类

    下面,我们只涉及MapReduce 1,而不涉及YARN. 当我们在写MapReduce程序的时候,通常,在main函数里,我们会像下面这样做.建立一个Job对象,设置它的JobName,然后配置输入 ...

  8. 从头开始编写一个Orchard网上商店模块(6) - 创建购物车服务和控制器

    原文地址: http://skywalkersoftwaredevelopment.net/blog/writing-an-orchard-webshop-module-from-scratch-pa ...

  9. C# 如何获取某个类型或类型实例对象的大小

    在统计类型或类型实例对象时,出了个异常: “不能作为非托管结构进行封送处理;无法计算有意义的大小或偏移量.” 后来查了一下,原来,我们创建的struct或是class都是属于复杂类型的.(纠正一下,如 ...

  10. How to detect and avoid memory and resources leaks in .NET applications

    By Fabrice Marguerie Despite what a lot of people believe, it's easy to introduce memory and resourc ...