java常用算法

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值