代码]选择排序
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
|
public
class
SelectionSort {
public
void
selectionSort(
int
[] array) {
int
temp;
for
(
int
i =
0
; i < array.length -
1
; i++) {
for
(
int
j = i +
1
; j <= array.length -
1
; j++) {
// 第i个和第j个比较j可以取到最后一位,所以要用j<=array.length-1
if
(array[i] > array[j]) {
// 注意和冒泡排序的区别,这里是i和j比较。
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
// 打印每趟排序结果
for
(
int
m =
0
; m <= array.length -
1
; m++) {
System.out.print(array[m] +
"\t"
);
}
System.out.println();
}
}
public
static
void
main(String[] args) {
SelectionSort selectionSort =
new
SelectionSort();
int
[] array = {
5
,
69
,
12
,
3
,
56
,
789
,
2
,
5648
,
23
};
selectionSort.selectionSort(array);
for
(
int
m =
0
; m <= array.length -
1
; m++) {
System.out.print(array[m] +
"\t"
);
}
}
}
|
2. [代码]插入排序
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
|
public
class
InsertSort {
public
void
insertSort(
int
[] array,
int
first,
int
last) {
int
temp, i, j;
for
(i = first +
1
; i <= last -
1
; i++) {
// 默认以第一个数为有序序列,后面的数为要插入的数。
temp = array[i];
j = i -
1
;
while
(j >= first && array[j] > temp) {
// 从后进行搜索如果搜索到的数小于temp则该数后移继续搜索,直到搜索到小于或等于temp的数即可
array[j +
1
] = array[j];
j--;
}
array[j +
1
] = temp;
// 打印每次排序结果
for
(
int
m =
0
; m <= array.length -
1
; m++) {
System.out.print(array[m] +
"\t"
);
}
System.out.println();
}
}
public
static
void
main(String[] args) {
InsertSort insertSort =
new
InsertSort();
int
[] array = {
5
,
69
,
12
,
3
,
56
,
789
,
2
,
5648
,
23
};
insertSort.insertSort(array,
0
, array.length);
// 注意此处是0-9而不是0-8
for
(
int
i =
0
; i <= array.length -
1
; i++) {
System.out.print(array[i] +
"\t"
);
}
}
}
|
3. [代码]归并排序
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
|
public
class
MergeSortTest {
public
void
sort(
int
[] array,
int
left,
int
right) {
if
(left >= right)
return
;
// 找出中间索引
int
center = (left + right) /
2
;
// 对左边数组进行递归
sort(array, left, center);
// 对右边数组进行递归
sort(array, center +
1
, right);
// 合并
merge(array, left, center, right);
// 打印每次排序结果
for
(
int
i =
0
; i < array.length; i++) {
System.out.print(array[i] +
"\t"
);
}
System.out.println();
}
/**
* 将两个数组进行归并,归并前面2个数组已有序,归并后依然有序
*
* @param array
* 数组对象
* @param left
* 左数组的第一个元素的索引
* @param center
* 左数组的最后一个元素的索引,center+1是右数组第一个元素的索引
* @param right
* 右数组最后一个元素的索引
*/
public
void
merge(
int
[] array,
int
left,
int
center,
int
right) {
// 临时数组
int
[] tmpArr =
new
int
[array.length];
// 右数组第一个元素索引
int
mid = center +
1
;
// third 记录临时数组的索引
int
third = left;
// 缓存左数组第一个元素的索引
int
tmp = left;
while
(left <= center && mid <= right) {
// 从两个数组中取出最小的放入临时数组
if
(array[left] <= array[mid]) {
tmpArr[third++] = array[left++];
}
else
{
tmpArr[third++] = array[mid++];
}
}
// 剩余部分依次放入临时数组(实际上两个while只会执行其中一个)
while
(mid <= right) {
tmpArr[third++] = array[mid++];
}
while
(left <= center) {
tmpArr[third++] = array[left++];
}
// 将临时数组中的内容拷贝回原数组中
// (原left-right范围的内容被复制回原数组)
while
(tmp <= right) {
array[tmp] = tmpArr[tmp++];
}
}
public
static
void
main(String[] args) {
int
[] array =
new
int
[] {
5
,
69
,
12
,
3
,
56
,
789
,
2
,
5648
,
23
};
MergeSortTest mergeSortTest =
new
MergeSortTest();
mergeSortTest.sort(array,
0
, array.length -
1
);
System.out.println(
"排序后的数组:"
);
for
(
int
m =
0
; m <= array.length -
1
; m++) {
System.out.print(array[m] +
"\t"
);
}
}
}
|
4. [代码]希尔排序
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
|
public
class
ShellSort {
public
void
shellSort(
int
[] array,
int
n) {
int
i, j, gap;
int
temp;
for
(gap = n /
2
; gap >
0
; gap /=
2
) {
// 计算gap大小
for
(i = gap; i < n; i++) {
// 将数据进行分组
for
(j = i - gap; j >=
0
&& array[j] > array[j + gap]; j -= gap) {
// 对每组数据进行插入排序
temp = array[j];
array[j] = array[j + gap];
array[j + gap] = temp;
}
// 打印每趟排序结果
for
(
int
m =
0
; m <= array.length -
1
; m++) {
System.out.print(array[m] +
"\t"
);
}
System.out.println();
}
}
}
public
static
void
main(String[] args) {
ShellSort shellSort =
new
ShellSort();
int
[] array = {
5
,
69
,
12
,
3
,
56
,
789
,
2
,
5648
,
23
};
shellSort.shellSort(array, array.length);
// 注意为数组的个数
for
(
int
m =
0
; m <= array.length -
1
; m++) {
System.out.print(array[m] +
"\t"
);
}
}
}
|
5. [代码]快速排序
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
|
public
class
QuickSort {
public
int
partition(
int
[] sortArray,
int
low,
int
height) {
int
key = sortArray[low];
// 刚开始以第一个数为标志数据
while
(low < height) {
while
(low < height && sortArray[height] >= key)
height--;
// 从后面开始找,找到比key值小的数为止
sortArray[low] = sortArray[height];
// 将该数放到key值的左边
while
(low < height && sortArray[low] <= key)
low++;
// 从前面开始找,找到比key值大的数为止
sortArray[height] = sortArray[low];
// 将该数放到key值的右边
}
sortArray[low] = key;
// 把key值填充到low位置,下次重新找key值
// 打印每次排序结果
for
(
int
i =
0
; i <= sortArray.length -
1
; i++) {
System.out.print(sortArray[i] +
"\t"
);
}
System.out.println();
return
low;
}
public
void
sort(
int
[] sortArray,
int
low,
int
height) {
if
(low < height) {
int
result = partition(sortArray, low, height);
sort(sortArray, low, result -
1
);
sort(sortArray, result +
1
, height);
}
}
public
static
void
main(String[] args) {
QuickSort quickSort =
new
QuickSort();
int
[] array = {
5
,
69
,
12
,
3
,
56
,
789
,
2
,
5648
,
23
};
for
(
int
i =
0
; i <= array.length -
1
; i++) {
System.out.print(array[i] +
"\t"
);
}
System.out.println();
quickSort.sort(array,
0
,
8
);
for
(
int
i =
0
; i <= array.length -
1
; i++) {
System.out.print(array[i] +
"\t"
);
}
}
}
|