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
class Solution {
public int[] sortArray(int[] nums) {
mergeSort(nums, nums.clone(), 0, nums.length - 1);
return nums;
}

private void swap(int[] nums, int i, int j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}

// Bubble Sort 最大值到最后一个位置, 次大到倒数第二个...
public void bubbleSort(int[] nums) {
int inPlaceCount = 0;
// We have nums.length numbers to process in order to let each of them be in
// the right place
for (int i = 0; i < nums.length - 1; i++) {
boolean isSorted = true;
// We don't bother the nums that are in place which is the last
// "inPlaceCount" nums.
for (int j = 0; j < nums.length - inPlaceCount - 1; j++) {
if (nums[j] > nums[j + 1]) {
isSorted = false;
swap(nums, j, j + 1);
}
}
// End the outer for loop earlier.
if (isSorted)
break;
inPlaceCount += 1;
}
return nums;
}

// Selection Sort 第0个位置最小的坐,第1个位置次小的坐...
public void selectionSort(int[] nums) {
for (int i = 0; i < nums.length - 1; i++) {
int minIdx = i;
for (int j = i; j < nums.length; j++) {
if (nums[j] < nums[minIdx]) {
minIdx = j;
}
}
swap(nums, minIdx, i);
}
return nums;
}

// Insertion Sort 当前位置的值比前一个小,那就换到前面,一直到换不动为止
public void insertionSort(int[] nums) {
for (int i = 1; i < nums.length; i++) {
for (int j = i; j > 0; j--) {
if (nums[j] < nums[j - 1]) {
swap(nums, j, j - 1);
} else {
break;
}
}
}
return nums;
}

// Quick Sort
private void quickSort(int[] nums, int start, int end) {
if (start >= end) {
return;
}
int pivot = start, left = start + 1, right = end;
while (left <= right) {
if (nums[left] > nums[pivot] && nums[right] < nums[pivot]) {
swap(nums, left, right);
left += 1;
right -= 1;
continue;
}
if (nums[left] <= nums[pivot]) {
left += 1;
}
if (nums[right] >= nums[pivot]) {
right -= 1;
}
}
swap(nums, pivot, right);
boolean isLeftShorter = right - start <= end - right ? true : false;
if (isLeftShorter) {
quickSort(nums, start, right - 1);
quickSort(nums, right + 1, end);
} else {
quickSort(nums, right + 1, end);
quickSort(nums, start, right - 1);
}
}

// Merge Sort 给一个范围, 能把这个范围内的数字给sort好. 注意nums和aux往下传递的时候要颠倒位置.
private void mergeSort(int[] nums, int[] aux, int start, int end) {
if (start >= end) {
return;
}
int mid = start + (end - start) / 2;
mergeSort(aux, nums, start, mid);
mergeSort(aux, nums, mid + 1, end);
merge(nums, aux, start, mid, end);
}

private void merge(int[] nums, int[] aux, int left, int middle, int right) {
int ptr = left, ptrOne = left, ptrTwo = middle + 1;
while (ptrOne <= middle && ptrTwo <= right) {
if (aux[ptrOne] <= aux[ptrTwo]) {
nums[ptr++] = aux[ptrOne++];
} else {
nums[ptr++] = aux[ptrTwo++];
}
}
while (ptrOne <= middle) {
nums[ptr++] = aux[ptrOne++];
}
while (ptrTwo <= right) {
nums[ptr++] = aux[ptrTwo++];
}
}
}