经典排序算法实现

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
157
158
159
160
161
162
163
164
165
166
167
168
/**
* @author corlymeng.com
* @date 2015年10月30日
*/
package com.corlymeng.sort;

public class AllSort {
//保存排序的结果
private int[] recType = null;

public AllSort(int[] arr) {
recType = arr;
}

////////////////////////插入排序 start/////////////////////////////
//插入排序
public void insertSort(int n) {
int i,j;
int tmp;
for(i=1; i<n; i++) {
tmp = recType[i];
j = i - 1;
while(j >= 0 && tmp < recType[j]) {
recType[j+1] = recType[j];
j--;
}
recType[j+1] = tmp;
}
}
//二分插入排序
public void insertSort1(int n) {
int i,j,low,high,mid;
int tmp;
for(i=1; i<n; i++){
tmp = recType[i];
low = 0;
high = i - 1;
while(low <= high){
mid = (low + high)/2;
if(tmp < recType[mid])
high = mid - 1;
else
low = mid + 1;
}
for(j=i-1; j>high+1; j--){
recType[j+1] = recType[j];
}
recType[high+1] = tmp;
}
}
//希尔排序
public void shellSort(int n){
int i,j,gap;
int tmp;
gap = n/2;
while(gap>0){
for(i = gap; i<n; i++){
tmp = recType[i];
j = i - gap;
while(j >= 0 && tmp < recType[j]){
recType[j+gap] = recType[j];
j = j-gap;
}
recType[j+gap] = tmp;
}
gap = gap/2;
}
}
////////////////////////插入排序 end/////////////////////////////

////////////////////////交换排序 start/////////////////////////////
//快速排序
public void quickSort(int s,int t){
int i = s,j = t;
int tmp;
if (s < t) {
tmp = recType[s];
while (i != j) {
while(j > i && tmp <= recType[j])
j--;
recType[i] = recType[j];
while(i < j && tmp > recType[i])
i++;
recType[j] = recType[i];
}
recType[i] = tmp;
quickSort(s, i-1);
quickSort(i+1, t);
}
}
//冒泡排序
public void bubbleSort() {
int i,j,tmp;
for(i=0; i<recType.length; i++){
for(j=i+1; j<recType.length; j++){
if (recType[i] > recType[j] ) {
tmp = recType[i];
recType[i] = recType[j];
recType[j] = tmp;
}
}
}
}

////////////////////////交换排序 end/////////////////////////////

////////////////////////选择排序 start///////////////////////////
//简单选择排序
public void selectionSort() {
int i,j,tmp;
for(i=1; i<recType.length; i++){
tmp = recType[i];
j=i-1;
while(j>=0 && tmp<recType[j]){
recType[j+1] = recType[j];
j--;
}
recType[j+1] = tmp;
}
}

//堆排序,排序是从数组的下标是1的元素开始排序的。
public void heapSort() {
for(int i=recType.length-1; i>0; i--){
buildHeap(i);
//每次排序完将将最大的元素调到最后
recType[0] = recType[i];
recType[i] = recType[1];
recType[1] = recType[0];
}
}
//调整二叉堆
private void percolateDown (int hole, int currentSize) {
int child;
int tmp = recType[hole];
for (; hole*2 <= currentSize; hole = child) {
child = hole*2;
if (child != currentSize && recType[child+1]>recType[child])
child++;
if (recType[child]>tmp)
recType[hole] = recType[child];
else
break;
}
recType[hole] = tmp;
}
private void buildHeap (int currentSize) {
for (int i=currentSize/2; i>0; i--)
percolateDown(i, currentSize);
}
////////////////////////选择排序 end/////////////////////////////

public void printArr(){
for(int i=0; i<recType.length; i++ ){
System.out.print(recType[i]+" ");
}
}

public static void main(String[] args) {
int[] arr = {0,5,2,3,8,9,7,5,4,11,25,22};
AllSort allSort = new AllSort(arr);
// allSort.quickSort(0, arr.length-1);
// allSort.selectionSort();
allSort.heapSort();
allSort.printArr();
}
}

欢迎关注我的其它发布渠道