常用排序算法
Xhofe Lv3

Sort

记录一些常用的排序算法

todo
  • 桶排序
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;
/**
* 复习排序算法
*/
public class Sort {
//比较类排序-------------------------------------------------------------
//交换类----------------------
/**
* 冒泡排序(稳定):每次走完一趟,最大的值就会到最后
*
* @param arr 数组
*/
public static void bubbleSort(int[] arr) {
int n = arr.length;
boolean flag;
for (int i = 0; i < n - 1; i++) {
flag = true;
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
flag = false;
swap(arr, j, j + 1);
}
}
if (flag) break;
}
}
/**
* 快速排序(不稳定)
*
* @param arr 数组
*/
public static void quickSort(int[] arr) {
quickSort(arr, 0, arr.length - 1);
}
public static void quickSort(int[] arr, int left, int right) {
if (left < right) {
int index = partition(arr, left, right);
quickSort(arr, left, index - 1);
quickSort(arr, index + 1, right);
}
}
public static int partition(int[] arr, int left, int right) {
int pivot = arr[left];
int index = left + 1;
for (int i = index; i <= right; i++) {
if (arr[i] < pivot) {
swap(arr, i, index);
++index;
}
}
swap(arr, left, index - 1);
return index - 1;
}
//插入类---------------------
/**
* 插入排序
*
* @param arr 数组
*/
public static void insertSort(int[] arr) {
if (arr == null || arr.length == 0) return;
for (int i = 1; i < arr.length; i++) {
int j = i;
while (j > 0 && arr[j] < arr[j - 1]) {
swap(arr, j, j - 1);
j--;
}
}
}
/**
* 希尔排序
*
* @param arr 数组
*/
public static void shellSort(int[] arr) {
int len = arr.length;
for (int gap = len / 2; gap > 0; gap = gap / 2) {
for (int i = gap; i < len; i++) {
int j = i;
int cur = arr[i];
while (j >= gap && cur < arr[j - gap]) {
arr[j] = arr[j - gap];
j = j - gap;
}
arr[j] = cur;
}
}
}
//选择类---------------------
//选择排序
public static void selectSort(int[] arr) {
int len = arr.length;
int minIndex;
for (int i = 0; i < len - 1; i++) {
minIndex = i;
for (int j = i + 1; j < len; j++) {
if (arr[j] < arr[minIndex]) minIndex = j;
}
swap(arr, i, minIndex);
}
}
// 堆排序
public static void heapify(int[] tree, int n, int i) {
if (i >= n) return;
int max = i;
int c1 = 2 * i + 1, c2 = 2 * i + 2;
if (c1 < n && tree[c1] > tree[max]) max = c1;
if (c2 < n && tree[c2] > tree[max]) max = c2;
if (max != i) {
swap(tree, max, i);
heapify(tree, n, max);
}
}
public static void buildHeap(int[] arr) {
int len = arr.length;
int last_parent = (len) / 2;
for (int i = last_parent; i >= 0; --i) {
heapify(arr, len, i);
}
}
public static void heapSort(int[] arr) {
int len = arr.length;
buildHeap(arr);
for (int i = len - 1; i >= 0; --i) {
swap(arr,0,i);
heapify(arr,i,0);
}
}
//归并类---------------------
//归并
public static void mergeSort(int[] arr) {
mergeSort(arr, 0, arr.length - 1);
}
public static void mergeSort(int[] arr, int left, int right) {
if (left >= right) return;
int mid = (left + right) / 2;
mergeSort(arr, left, mid);
mergeSort(arr, mid + 1, right);
int[] tmp = new int[right - left + 1];
int index = 0, index1 = left, index2 = mid + 1;
while (index1 <= mid && index2 <= right) {
if (arr[index1] < arr[index2]) {
tmp[index++] = arr[index1++];
} else tmp[index++] = arr[index2++];
}
while (index1 <= mid) tmp[index++] = arr[index1++];
while (index2 <= right) tmp[index++] = arr[index2++];
System.arraycopy(tmp, 0, arr, left, tmp.length);
}

//非比较排序--------------------------------------------------------------
//计数排序
public static void countingSort(int[] arr, int maxValue) {
int[] counts = new int[maxValue + 1];
for (int i : arr) {
counts[i]++;
}
int index = 0;
for (int i = 0; i < maxValue + 1; i++) {
int count = counts[i];
while (count > 0) {
count--;
arr[index++] = i;
}
}
}
//TODO 桶排序
public static void bucketSort(int[] arr, int bucketSize) {
int maxValue = arr[0], minValue = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] > maxValue) maxValue = arr[i];
else if (arr[i] < minValue) minValue = arr[i];
}
int bucketCount = (maxValue - minValue) / bucketSize + 1;
}
//基数排序
public static void radixSort(int[] arr, int maxDigit) {
Map<Integer, Queue<Integer>> map = new HashMap<>();
int mod = 10, dev = 1;
for (int i = 0; i < maxDigit; i++, dev *= 10, mod *= 10) {
for (int value : arr) {
int bucket = (value % mod) / dev;
if (!map.containsKey(bucket)) {
map.put(bucket, new LinkedList<>());
}
map.get(bucket).add(value);
}
int pos = 0;
for (int j = 0; j < 10; j++) {
if (map.containsKey(j)) {
int len = map.get(j).size();
for (int k = 0; k < len; k++) {
arr[pos++] = map.get(j).poll();
}
}
}
}
}
/**
* 交换数组的值
*
* @param arr 数组
* @param i 下标
* @param j 另一个下标
*/
public static void swap(int[] arr, int i, int j) {
int tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
public static void main(String[] args) {
int[] arr = new int[]{3, 1, 4, 5, 11, 2};
// Sort.radixSort(arr, 2);
heapSort(arr);
for (int i : arr) {
System.out.printf("%d ", i);
}
}
}
  • 本文标题:常用排序算法
  • 本文作者:Xhofe
  • 创建时间:2020-04-01 17:20:18
  • 本文链接:https://nn.ci/posts/commonly-used-sort.html
  • 版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
 评论