1、java 的几种排序方式用 Java 语言实现的各种排序,包括插入排序、冒泡排序、选择排序、Shell 排序、快速排序、归并排序、堆排序、SortUtil 等。插入排序:package org.rut.util.algorithm.support;import org.rut.util.algorithm.SortUtil;/* author treeroot* since 2006-2-2* version 1.0*/public class InsertSort implements SortUtil.Sort/* (non-Javadoc)* see org.rut.util.algor
2、ithm.SortUtil.Sort#sort(int)*/public void sort(int data) int temp;for(int i=1;i for(int j=i;(j0) 冒泡排序:package org.rut.util.algorithm.support;import org.rut.util.algorithm.SortUtil;/* author treeroot* since 2006-2-2* version 1.0*/public class BubbleSort implements SortUtil.Sort/* (non-Javadoc)* see o
3、rg.rut.util.algorithm.SortUtil.Sort#sort(int)*/public void sort(int data) int temp;for(int i=0;i for(int j=data.length-1;ji;j-)if(dataj SortUtil.swap(data,j,j-1);选择排序:package org.rut.util.algorithm.support;import org.rut.util.algorithm.SortUtil;/* author treeroot* since 2006-2-2* version 1.0*/public
4、 class SelectionSort implements SortUtil.Sort /* (non-Javadoc)* * see org.rut.util.algorithm.SortUtil.Sort#sort(int)*/public void sort(int data) int temp;for (int i = 0; i i; j-) if (dataj 2;i/=2)for(int j=0;j insertSort(data,j,i);insertSort(data,0,1);/* param data* param j* param i*/private void in
5、sertSort(int data, int start, int inc) int temp;for(int i=start+inc;i for(int j=i;(j=inc)快速排序:package org.rut.util.algorithm.support;import org.rut.util.algorithm.SortUtil;/* author treeroot* since 2006-2-2* version 1.0*/public class QuickSort implements SortUtil.Sort/* (non-Javadoc)* see org.rut.ut
6、il.algorithm.SortUtil.Sort#sort(int)*/public void sort(int data) quickSort(data,0,data.length-1); private void quickSort(int data,int i,int j)int pivotIndex=(i+j)/2;/swapSortUtil.swap(data,pivotIndex,j);int k=partition(data,i-1,j,dataj);SortUtil.swap(data,k,j);if(k-i)1) quickSort(data,i,k-1);if(j-k)
7、1) quickSort(data,k+1,j);/* param data* param i* param j* return*/private int partition(int data, int l, int r,int pivot) dowhile(data+l while(r!=0)SortUtil.swap(data,l,r);while(l SortUtil.swap(data,l,r); return l; 改进后的快速排序:package org.rut.util.algorithm.support;import org.rut.util.algorithm.SortUti
8、l;/* author treeroot* since 2006-2-2* version 1.0*/public class ImprovedQuickSort implements SortUtil.Sort private static int MAX_STACK_SIZE=4096;private static int THRESHOLD=10;/* (non-Javadoc)* see org.rut.util.algorithm.SortUtil.Sort#sort(int)*/public void sort(int data) int stack=new intMAX_STAC
9、K_SIZE;int top=-1;int pivot;int pivotIndex,l,r;stack+top=0;stack+top=data.length-1;while(top0)int j=stacktop-;int i=stacktop-;pivotIndex=(i+j)/2;pivot=datapivotIndex;SortUtil.swap(data,pivotIndex,j);/partitionl=i-1;r=j;dowhile(data+l while(r!=0)SortUtil.swap(data,l,r);while(l SortUtil.swap(data,l,r)
10、;SortUtil.swap(data,l,j);if(l-i)THRESHOLD)stack+top=i;stack+top=l-1;if(j-l)THRESHOLD)stack+top=l+1;stack+top=j;/new InsertSort().sort(data);insertSort(data);/* param data*/private void insertSort(int data) int temp;for(int i=1;i for(int j=i;(j0) 归并排序:package org.rut.util.algorithm.support;import org
11、.rut.util.algorithm.SortUtil;/* author treeroot* since 2006-2-2* version 1.0*/public class MergeSort implements SortUtil.Sort/* (non-Javadoc)* see org.rut.util.algorithm.SortUtil.Sort#sort(int)*/public void sort(int data) int temp=new intdata.length;mergeSort(data,temp,0,data.length-1);private void
12、mergeSort(int data,int temp,int l,int r)int mid=(l+r)/2;if(l=r) return ;mergeSort(data,temp,l,mid);mergeSort(data,temp,mid+1,r);for(int i=l;ir)datacur=tempi1+;else if(tempi1 datacur=tempi1+;elsedatacur=tempi2+; 改进后的归并排序:package org.rut.util.algorithm.support;import org.rut.util.algorithm.SortUtil;/*
13、 author treeroot* since 2006-2-2* version 1.0*/public class ImprovedMergeSort implements SortUtil.Sort private static final int THRESHOLD = 10;/* (non-Javadoc)* * see org.rut.util.algorithm.SortUtil.Sort#sort(int)*/public void sort(int data) int temp=new intdata.length;mergeSort(data,temp,0,data.len
14、gth-1);private void mergeSort(int data, int temp, int l, int r) int i, j, k;int mid = (l + r) / 2;if (l = r)return;if (mid - l) = THRESHOLD)mergeSort(data, temp, l, mid);elseinsertSort(data, l, mid - l + 1);if (r - mid) THRESHOLD)mergeSort(data, temp, mid + 1, r);elseinsertSort(data, mid + 1, r - mi
15、d);for (i = l; i start) 堆排序:package org.rut.util.algorithm.support;import org.rut.util.algorithm.SortUtil;/* author treeroot* since 2006-2-2* version 1.0*/public class HeapSort implements SortUtil.Sort/* (non-Javadoc)* see org.rut.util.algorithm.SortUtil.Sort#sort(int)*/public void sort(int data) Ma
16、xHeap h=new MaxHeap();h.init(data);for(int i=0;i h.remove();System.arraycopy(h.queue,1,data,0,data.length);private static class MaxHeap void init(int data)this.queue=new intdata.length+1;for(int i=0;i queue+size=data;fixUp(size);private int size=0;private int queue;public int get() return queue1;pub
17、lic void remove() SortUtil.swap(queue,1,size-);fixDown(1);/fixdownprivate void fixDown(int k) int j;while (j = k queuej) /不用交换break;SortUtil.swap(queue,j,k);k = j;private void fixUp(int k) while (k 1) int j = k 1;if (queuejqueuek)break;SortUtil.swap(queue,j,k);k = j;SortUtil:package org.rut.util.alg
18、orithm;import org.rut.util.algorithm.support.BubbleSort;import org.rut.util.algorithm.support.HeapSort;import org.rut.util.algorithm.support.ImprovedMergeSort;import org.rut.util.algorithm.support.ImprovedQuickSort;import org.rut.util.algorithm.support.InsertSort;import org.rut.util.algorithm.suppor
19、t.MergeSort;import org.rut.util.algorithm.support.QuickSort;import org.rut.util.algorithm.support.SelectionSort;import org.rut.util.algorithm.support.ShellSort;/* author treeroot* since 2006-2-2* version 1.0*/public class SortUtil public final static int INSERT = 1;public final static int BUBBLE = 2
20、;public final static int SELECTION = 3;public final static int SHELL = 4;public final static int QUICK = 5;public final static int IMPROVED_QUICK = 6;public final static int MERGE = 7;public final static int IMPROVED_MERGE = 8;public final static int HEAP = 9;public static void sort(int data) sort(d
21、ata, IMPROVED_QUICK);private static String name=“insert“, “bubble“, “selection“, “shell“, “quick“, “improved_quick“, “merge“, “improved_merge“, “heap“;private static Sort impl=new Sortnew InsertSort(),new BubbleSort(),new SelectionSort(),new ShellSort(),new QuickSort(),new ImprovedQuickSort(),new Me
22、rgeSort(),new ImprovedMergeSort(),new HeapSort();public static String toString(int algorithm)return namealgorithm-1;public static void sort(int data, int algorithm) implalgorithm-1.sort(data);public static interface Sort public void sort(int data);public static void swap(int data, int i, int j) int temp = data;data = dataj;dataj = temp;