高级码农
命运在自己手里
用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.algorithm.SortUtil.Sort#sort(int[])
     
*/

    
public void sort(int[] data) {
        
int temp;
        
for(int i=1;i<data.length;i++){
            
for(int j=i;(j>0)&&(data[j]<data[j-1]);j--){
                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 class BubbleSort 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<data.length;i++){
            
for(int j=data.length-1;j>i;j--){
                
if(data[j]<data[j-1]){
                    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 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 < data.length; i++{
            
int lowIndex = i;
            
for (int j = data.length - 1; j > i; j--{
                
if (data[j] < data[lowIndex]) {
                    lowIndex 
= j;
                }

            }

            SortUtil.swap(data,i,lowIndex);
        }

    }


}
Shell排序:
 1package org.rut.util.algorithm.support;
 2
 3import org.rut.util.algorithm.SortUtil;
 4
 5/**
 6 * @author treeroot
 7 * @since 2006-2-2
 8 * @version 1.0
 9 */

10public class ShellSort implements SortUtil.Sort{
11
12    /* (non-Javadoc)
13     * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
14     */

15    public void sort(int[] data) {
16        for(int i=data.length/2;i>2;i/=2){
17            for(int j=0;j<i;j++){
18                insertSort(data,j,i);
19            }

20        }

21        insertSort(data,0,1);
22    }

23
24    /**
25     * @param data
26     * @param j
27     * @param i
28     */

29    private void insertSort(int[] data, int start, int inc) {
30        int temp;
31        for(int i=start+inc;i<data.length;i+=inc){
32            for(int j=i;(j>=inc)&&(data[j]<data[j-inc]);j-=inc){
33                SortUtil.swap(data,j,j-inc);
34            }

35        }

36    }

37
38}


快速排序:
 1package org.rut.util.algorithm.support;
 2
 3import org.rut.util.algorithm.SortUtil;
 4
 5/**
 6 * @author treeroot
 7 * @since 2006-2-2
 8 * @version 1.0
 9 */

10public class QuickSort implements SortUtil.Sort{
11
12    /* (non-Javadoc)
13     * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
14     */

15    public void sort(int[] data) {
16        quickSort(data,0,data.length-1);        
17    }

18    private void quickSort(int[] data,int i,int j){
19        int pivotIndex=(i+j)/2;
20        //swap
21        SortUtil.swap(data,pivotIndex,j);
22        
23        int k=partition(data,i-1,j,data[j]);
24        SortUtil.swap(data,k,j);
25        if((k-i)>1) quickSort(data,i,k-1);
26        if((j-k)>1) quickSort(data,k+1,j);
27        
28    }

29    /**
30     * @param data
31     * @param i
32     * @param j
33     * @return
34     */

35    private int partition(int[] data, int l, int r,int pivot) {
36        do{
37           while(data[++l]<pivot);
38           while((r!=0)&&data[--r]>pivot);
39           SortUtil.swap(data,l,r);
40        }

41        while(l<r);
42        SortUtil.swap(data,l,r);        
43        return l;
44    }

45
46}

改进后的快速排序:
 1package org.rut.util.algorithm.support;
 2
 3import org.rut.util.algorithm.SortUtil;
 4
 5/**
 6 * @author treeroot
 7 * @since 2006-2-2
 8 * @version 1.0
 9 */

10public class ImprovedQuickSort implements SortUtil.Sort {
11
12    private static int MAX_STACK_SIZE=4096;
13    private static int THRESHOLD=10;
14    /* (non-Javadoc)
15     * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
16     */

17    public void sort(int[] data) {
18        int[] stack=new int[MAX_STACK_SIZE];
19        
20        int top=-1;
21        int pivot;
22        int pivotIndex,l,r;
23        
24        stack[++top]=0;
25        stack[++top]=data.length-1;
26        
27        while(top>0){
28            int j=stack[top--];
29            int i=stack[top--];
30            
31            pivotIndex=(i+j)/2;
32            pivot=data[pivotIndex];
33            
34            SortUtil.swap(data,pivotIndex,j);
35            
36            //partition
37            l=i-1;
38            r=j;
39            do{
40                while(data[++l]<pivot);
41                while((r!=0)&&(data[--r]>pivot));
42                SortUtil.swap(data,l,r);
43            }

44            while(l<r);
45            SortUtil.swap(data,l,r);
46            SortUtil.swap(data,l,j);
47            
48            if((l-i)>THRESHOLD){
49                stack[++top]=i;
50                stack[++top]=l-1;
51            }

52            if((j-l)>THRESHOLD){
53                stack[++top]=l+1;
54                stack[++top]=j;
55            }

56            
57        }

58        //new InsertSort().sort(data);
59        insertSort(data);
60    }

61    /**
62     * @param data
63     */

64    private void insertSort(int[] data) {
65        int temp;
66        for(int i=1;i<data.length;i++){
67            for(int j=i;(j>0)&&(data[j]<data[j-1]);j--){
68                SortUtil.swap(data,j,j-1);
69            }

70        }
       
71    }

72
73}

归并排序:
 1package org.rut.util.algorithm.support;
 2
 3import org.rut.util.algorithm.SortUtil;
 4
 5/**
 6 * @author treeroot
 7 * @since 2006-2-2
 8 * @version 1.0
 9 */

10public class MergeSort implements SortUtil.Sort{
11
12    /* (non-Javadoc)
13     * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
14     */

15    public void sort(int[] data) {
16        int[] temp=new int[data.length];
17        mergeSort(data,temp,0,data.length-1);
18    }

19    
20    private void mergeSort(int[] data,int[] temp,int l,int r){
21        int mid=(l+r)/2;
22        if(l==r) return ;
23        mergeSort(data,temp,l,mid);
24        mergeSort(data,temp,mid+1,r);
25        for(int i=l;i<=r;i++){
26            temp[i]=data[i];
27        }

28        int i1=l;
29        int i2=mid+1;
30        for(int cur=l;cur<=r;cur++){
31            if(i1==mid+1)
32                data[cur]=temp[i2++];
33            else if(i2>r)
34                data[cur]=temp[i1++];
35            else if(temp[i1]<temp[i2])
36                data[cur]=temp[i1++];
37            else
38                data[cur]=temp[i2++];            
39        }

40    }

41
42}

改进后的归并排序:

 1package org.rut.util.algorithm.support;
 2
 3import org.rut.util.algorithm.SortUtil;
 4
 5/**
 6 * @author treeroot
 7 * @since 2006-2-2
 8 * @version 1.0
 9 */

10public class ImprovedMergeSort implements SortUtil.Sort {
11
12    private static final int THRESHOLD = 10;
13
14    /*
15     * (non-Javadoc)
16     * 
17     * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
18     */

19    public void sort(int[] data) {
20        int[] temp=new int[data.length];
21        mergeSort(data,temp,0,data.length-1);
22    }

23
24    private void mergeSort(int[] data, int[] temp, int l, int r) {
25        int i, j, k;
26        int mid = (l + r) / 2;
27        if (l == r)
28            return;
29        if ((mid - l) >= THRESHOLD)
30            mergeSort(data, temp, l, mid);
31        else
32            insertSort(data, l, mid - l + 1);
33        if ((r - mid) > THRESHOLD)
34            mergeSort(data, temp, mid + 1, r);
35        else
36            insertSort(data, mid + 1, r - mid);
37
38        for (i = l; i <= mid; i++{
39            temp[i] = data[i];
40        }

41        for (j = 1; j <= r - mid; j++{
42            temp[r - j + 1= data[j + mid];
43        }

44        int a = temp[l];
45        int b = temp[r];
46        for (i = l, j = r, k = l; k <= r; k++{
47            if (a < b) {
48                data[k] = temp[i++];
49                a = temp[i];
50            }
 else {
51                data[k] = temp[j--];
52                b = temp[j];
53            }

54        }

55    }

56
57    /**
58     * @param data
59     * @param l
60     * @param i
61     */

62    private void insertSort(int[] data, int start, int len) {
63        for(int i=start+1;i<start+len;i++){
64            for(int j=i;(j>start) && data[j]<data[j-1];j--){
65                SortUtil.swap(data,j,j-1);
66            }

67        }

68    }

堆排序:
 1package org.rut.util.algorithm.support;
 2
 3import org.rut.util.algorithm.SortUtil;
 4
 5/**
 6 * @author treeroot
 7 * @since 2006-2-2
 8 * @version 1.0
 9 */

10public class HeapSort implements SortUtil.Sort{
11
12    /* (non-Javadoc)
13     * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
14     */

15    public void sort(int[] data) {
16        MaxHeap h=new MaxHeap();
17        h.init(data);
18        for(int i=0;i<data.length;i++)
19            h.remove();
20        System.arraycopy(h.queue,1,data,0,data.length);
21    }

22
23     private static class MaxHeap{         
24        
25        void init(int[] data){
26            this.queue=new int[data.length+1];
27            for(int i=0;i<data.length;i++){
28                queue[++size]=data[i];
29                fixUp(size);
30            }

31        }

32         
33        private int size=0;
34
35        private int[] queue;
36                
37        public int get() {
38            return queue[1];
39        }

40
41        public void remove() {
42            SortUtil.swap(queue,1,size--);
43            fixDown(1);
44        }

45        //fixdown
46        private void fixDown(int k) {
47            int j;
48            while ((j = k << 1<= size) {
49                if (j < size && queue[j]<queue[j+1])
50                    j++
51                if (queue[k]>queue[j]) //不用交换
52                    break;
53                SortUtil.swap(queue,j,k);
54                k = j;
55            }

56        }

57        private void fixUp(int k) {
58            while (k > 1{
59                int j = k >> 1;
60                if (queue[j]>queue[k])
61                    break;
62                SortUtil.swap(queue,j,k);
63                k = j;
64            }

65        }

66
67    }

68
69}


SortUtil:
 1package org.rut.util.algorithm;
 2
 3import org.rut.util.algorithm.support.BubbleSort;
 4import org.rut.util.algorithm.support.HeapSort;
 5import org.rut.util.algorithm.support.ImprovedMergeSort;
 6import org.rut.util.algorithm.support.ImprovedQuickSort;
 7import org.rut.util.algorithm.support.InsertSort;
 8import org.rut.util.algorithm.support.MergeSort;
 9import org.rut.util.algorithm.support.QuickSort;
10import org.rut.util.algorithm.support.SelectionSort;
11import org.rut.util.algorithm.support.ShellSort;
12
13/**
14 * @author treeroot
15 * @since 2006-2-2
16 * @version 1.0
17 */

18public class SortUtil {
19    public final static int INSERT = 1;
20    public final static int BUBBLE = 2;
21    public final static int SELECTION = 3;
22    public final static int SHELL = 4;
23    public final static int QUICK = 5;
24    public final static int IMPROVED_QUICK = 6;
25    public final static int MERGE = 7;
26    public final static int IMPROVED_MERGE = 8;
27    public final static int HEAP = 9;
28
29    public static void sort(int[] data) {
30        sort(data, IMPROVED_QUICK);
31    }

32    private static String[] name={
33            "insert""bubble""selection""shell""quick""improved_quick""merge""improved_merge""heap"
34    }
;
35    
36    private static Sort[] impl=new Sort[]{
37            new InsertSort(),
38            new BubbleSort(),
39            new SelectionSort(),
40            new ShellSort(),
41            new QuickSort(),
42            new ImprovedQuickSort(),
43            new MergeSort(),
44            new ImprovedMergeSort(),
45            new HeapSort()
46    }
;
47
48    public static String toString(int algorithm){
49        return name[algorithm-1];
50    }

51    
52    public static void sort(int[] data, int algorithm) {
53        impl[algorithm-1].sort(data);
54    }

55
56    public static interface Sort {
57        public void sort(int[] data);
58    }

59
60    public static void swap(int[] data, int i, int j) {
61        int temp = data[i];
62        data[i] = data[j];
63        data[j] = temp;
64    }

65}

posted on 2006-04-20 00:48  高级码农  阅读(231)  评论(0)    收藏  举报

我的照片