主页 > 程序人生 > 自己写着玩的java排序类

自己写着玩的java排序类

突然有欲望打代码。。。于是结合了以前的一些代码,把自己会写的排序整合了一下,写了个排序类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
 
public class Sortman {
 
	/**
	 * 冒泡排序
	 * @param Comparable数组引用
	 * @param start 其实位置
	 * @param end  终止位置
	 */
	public void bubbleSrot(Comparable[] a,int start,int end){
		for(int i=end;i>start;i--){
			for(int j=start+1;j<=i;j++){
				if(a[j-1].compareTo(a[j])>0){
					Comparable temp=a[j-1];
					a[j-1]=a[j];
					a[j]=temp;
				}
			}
		}
	}
	/**
	 * 选择排序
	 * @param Comparable数组引用
	 * @param start 其实位置
	 * @param end  终止位置
	 */
	public void selectionSort(Comparable[] a,int start,int end){
		for(int i=start;i<end;i++){
			int p=i;
			for(int j=i+1;j<=end;j++){
				if(a[j].compareTo(a[p])<0)
					p=j;
			}
			if(p!=i){
				Comparable temp=a[i];
				a[i]=a[p];
				a[p]=temp;
			}
		}
	}
	/**
	 * 插入排序
	 * @param Comparable数组引用
	 * @param start 其实位置
	 * @param end  终止位置
	 */
	public void insertionSort(Comparable[] a,int start,int end){
		for(int i=start+1;i<=end;i++){
			Comparable val=a[i];
			int j=i;
			while(j>start&&val.compareTo(a[j-1])<0){
				a[j]=a[j-1];
				j--;
			}
			a[j]=val;
		}
	}
 
 
	private int split(int low, int high,Comparable[] a)
    {
        Comparable t;
        t=a[low];
        while(low<high)
        {
            while(a[high].compareTo(t)>0&&low<high)
            {
                high--;
            }
            if(low==high)
                break;
            else
            {
                a[low]=a[high];
                low++;
            }
            while(a[low].compareTo(t)<0&&low<high)
            {
                low++;
            }
            if(low==high)
                break;
            else
            {
                a[high]=a[low];
                high--;
            }
        }
        a[low]=t;
        return low;
    }
	/**
	 * 快速排序
	 * @param Comparable数组引用
	 * @param start 其实位置
	 * @param end  终止位置
	 */
    public  void quickSort(Comparable[] a,int start,int end)
    {
        int p;
        if(start<end)
        {
            p=split(start,end,a);
            quickSort(a,start,p-1);
            quickSort(a,p+1,end);
 
        }
    }
    private  void add(Comparable[] a,int l1,int h1,int l2,int h2)
    {
    	Comparable[] t;
        int i,j,k;
        i=l1;
        j=l2;
        k=0;
        t=new Comparable[h2-l1+1];
        while(i<=h1&&j<=h2)
        {
            if(a[i].compareTo(a[j])<0)
            {
                t[k++]=a[i++];
            }else
            {
                t[k++]=a[j++];
            }
        }
        while(i<=h1)
        {
            t[k++]=a[i++];
        }
        while(j<=h2)
            t[k++]=a[j++];
        for(i=l1,j=0;i<=h2;i++,j++)
        {
            a[i]=t[j];
        }
    }
    /**
	 * 归并排序
	 * @param Comparable数组引用
	 * @param start 其实位置
	 * @param end  终止位置
	 */
    public   void mergeSort(Comparable[] a,int low,int high)
    {
        int mid=(low+high)/2;
        if(low<high)
        {
        	mergeSort(a,low,mid);
        	mergeSort(a,mid+1,high);
            add(a,low,mid,mid+1,high);
 
        }
    }
 
}

相关日志

, , ,

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

*

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

引用:0

下面所列的是引用到本博客的链接
自己写着玩的java排序类 来自 混沌的云
顶部