【Leetcode算法题】912. 排序数组

By Long Luo

912. 排序数组题目如下:

  1. 排序数组
    给你一个整数数组 nums,请你将该数组升序排列。

示例 1:
输入:nums = [5,2,3,1]
输出:[1,2,3,5]

示例 2:
输入:nums = [5,1,1,2,0,0]
输出:[0,0,1,1,2,5]

提示:
1 <= nums.length <= 50000
-50000 <= nums[i] <= 50000

方法一:Bubble Sort

思路与算法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public static int[] bubbleSort(int[] nums) {
if (nums == null || nums.length <= 1) {
return nums;
}

int len = nums.length;
for (int i = len - 1; i >= 0; i--) {
boolean isSorted = true;
for (int j = 0; j < i; j++) {
if (nums[j] > nums[j + 1]) {
isSorted = false;
int temp = nums[j];
nums[j] = nums[j + 1];
nums[j + 1] = temp;
}
}
if (isSorted) {
break;
}
}

return nums;
}

复杂度分析:

  • 时间复杂度:O(N2)O(N^2),其中NN是数组nums\textit{nums}的长度。
  • 空间复杂度:O(1)O(1)

方法二:Select Sort

思路与算法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public static int[] selectSort(int[] nums) {
if (nums == null || nums.length <= 1) {
return nums;
}

int len = nums.length;
for (int i = 0; i < len; i++) {
for (int j = i + 1; j < len; j++) {
if (nums[j] < nums[i]) {
int temp = nums[j];
nums[j] = nums[i];
nums[i] = temp;
}
}
}

return nums;
}

复杂度分析:

  • 时间复杂度:O(N2)O(N^2),其中NN是数组nums\textit{nums}的长度。
  • 空间复杂度:O(1)O(1)

方法三:Insert Sort

思路与算法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public static int[] insertSort(int[] nums) {
if (nums == null || nums.length <= 1) {
return nums;
}

int len = nums.length;
for (int i = 1; i < len; i++) {
for (int j = i - 1; j >= 0; j--) {
if (nums[j] > nums[j + 1]) {
int temp = nums[j + 1];
nums[j + 1] = nums[j];
nums[j] = temp;
}
}
}

return nums;
}

复杂度分析:

  • 时间复杂度:O(N2)O(N^2),其中NN是数组nums\textit{nums}的长度。
  • 空间复杂度:O(1)O(1)

方法四:Shell Sort

思路与算法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public static int[] shellSort(int[] nums) {
if (nums == null || nums.length <= 1) {
return nums;
}

int len = nums.length;
for (int gap = len / 2; gap >= 1; gap /= 2) {
for (int i = gap; i < len; i++) {
int j = i;
while (j - gap >= 0 && nums[j] < nums[j - gap]) {
int temp = nums[j];
nums[j] = nums[j - gap];
nums[j - gap] = temp;
j -= gap;
}
}
}

return nums;
}

复杂度分析:

  • 时间复杂度:O(N2)O(N^2),其中NN是数组nums\textit{nums}的长度。
  • 空间复杂度:O(1)O(1)

方法五:Heap Sort

思路与算法:

1

复杂度分析:

  • 时间复杂度:O(N2)O(N^2),其中NN是数组nums\textit{nums}的长度。
  • 空间复杂度:O(1)O(1)

方法六:Merge Sort

思路与算法:

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
public int[] mergeSort(int[] nums) {
if (nums == null || nums.length <= 1) {
return nums;
}

int len = nums.length;
mergeSort(nums, 0, len - 1);
return nums;
}

public void mergeSort(int[] nums, int left, int right) {
if (left < right) {
int mid = left + (right - left) / 2;
mergeSort(nums, left, mid);
mergeSort(nums, mid + 1, right);
merge(nums, left, mid, right);
}
}

public void merge(int[] nums, int left, int mid, int right) {
int i = left;
int j = mid + 1;
int[] temp = new int[nums.length];
int t = 0;
while (i <= mid && j <= right) {
if (nums[i] >= nums[j]) {
temp[t++] = nums[j++];
} else {
temp[t++] = nums[i++];
}
}

while (i <= mid) {
temp[t++] = nums[i++];
}

while (j <= right) {
temp[t++] = nums[j++];
}

t = 0;
while (left <= right) {
nums[left++] = temp[t++];
}
}

复杂度分析:

  • 时间复杂度:O(N2)O(N^2),其中NN是数组nums\textit{nums}的长度。
  • 空间复杂度:O(1)O(1)

方法七:Quick Sort

思路与算法:

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

public void quickSort(int[] nums, int low, int high) {
if (low < high) {
int pos = partition(nums, low, high);
quickSort(nums, low, pos - 1);
quickSort(nums, pos + 1, high);
}
}

public int partition(int[] nums, int low, int high) {
int pivot = nums[low];
while (low < high) {
while (low < high && nums[high] > pivot) {
high--;
}
if (low < high) {
nums[low] = nums[high];
}
while (low < high && nums[low] < pivot) {
low++;
}
if (low < high) {
nums[high] = nums[low];
}
}
nums[low] = pivot;
return low;
}

复杂度分析:

  • 时间复杂度:O(N2)O(N^2),其中NN是数组nums\textit{nums}的长度。
  • 空间复杂度:O(1)O(1)