[LeetCode] 912. Sort an Array 数组排序
2023-09-11 14:21:36 时间
Given an array of integers `nums`, sort the array in ascending order.
Example 1:
Input: [5,2,3,1]
Output: [1,2,3,5]
Example 2:
Input: [5,1,1,2,0,0]
Output: [0,0,1,1,2,5]
Note:
1 <= A.length <= 10000
-50000 <= A[i] <= 50000
这道题让我们给数组排序,在平时刷其他题的时候,遇到要排序的时候,一般都会调用系统自带的排序函数,像 C++ 中直接就调用 sort 函数即可,但是这道题考察的就是排序,再调用系统的排序函数就有些说不过去了。这里需要自己实现排序功能,常见排序方法有很多,插入排序,选择排序,堆排序,快速排序,冒泡排序,归并排序,桶排序等等。它们的时间复杂度不尽相同,这道题貌似对于平方级复杂度的排序方法会超时,所以只能使用那些速度比较快的排序方法啦。题目给定了每个数字的范围是 [-50000, 50000],并不是特别大,这里可以使用记数排序 Count Sort,在 LeetCode 中也有直接利用这个解法的题[Sort Colors](http://www.cnblogs.com/grandyang/p/4341243.html),建立一个大小为 100001 的数组 count,然后统计 nums 中每个数字出现的个数,然后再从0遍历到 100000,对于每个遍历到的数字i,若个数不为0,则加入 count 数组中对应个数的 i-50000 到结果数组中,这里的 50000 是 offset,因为数组下标不能为负数,在开始统计个数的时候,每个数字都加上了 50000,那么最后组成有序数组的时候就要减去,参见代码如下:
解法一:
class Solution {
public:
vector<int> sortArray(vector<int>& nums) {
int n = nums.size(), j = 0;
vector<int> res(n), count(100001);
for (int num : nums) ++count[num + 50000];
for (int i = 0; i < count.size(); ++i) {
while (count[i]-- > 0) {
res[j++] = i - 50000;
}
}
return res;
}
};
下面就是大名鼎鼎的快速排序了 Quick Sort,貌似 STL 中的内置 sort 函数就是基于快速排序的,只不过这里要自己写而已。在 LeetCode 中也有一道使用这个算法思想的题 [Kth Largest Element in an Array](http://www.cnblogs.com/grandyang/p/4539757.html)。快排的精髓在于选一个 pivot,然后将所有小于 pivot 的数字都放在左边,大于 pivot 的数字都放在右边,等于的话放哪边都行。但是此时左右两边的数组各自都不一定是有序的,需要再各自调用相同的递归,直到细分到只有1个数字的时候,再返回的时候就都是有序的了,参见代码如下:
解法二:
class Solution {
public:
vector<int> sortArray(vector<int>& nums) {
quickSort(nums, 0, (int)nums.size() - 1);
return nums;
}
void quickSort(vector<int>& nums, int start, int end) {
if (start >= end) return;
int pivot = nums[start], i = start + 1, j = end;
while (i <= j) {
if (nums[i] > pivot && nums[j] < pivot) {
swap(nums[i++], nums[j--]);
}
if (nums[i] <= pivot) ++i;
if (nums[j] >= pivot) --j;
}
swap(nums[start], nums[j]);
quickSort(nums, start, j - 1);
quickSort(nums, j + 1, end);
}
};
我们也可以使用混合排序 Merge Sort,在 LeetCode 中也有一道使用这个思想的题 [Count of Range Sum](http://www.cnblogs.com/grandyang/p/5162678.html)。混合排序的思想跟快速排序比较类似,但也不完全一样,这里其实是一种先对半分,一直不停的对半分,直到分到只有一个数字的时候返回,然后在返回的途中进行合并,合并的时候用到了一个临时数组 tmp,先将区间 [start, end] 中的数字按顺序存入这个临时数组 tmp 中,然后再覆盖原数组中的对应位置即可,参见代码如下:
解法三:
class Solution {
public:
vector<int> sortArray(vector<int>& nums) {
mergeSort(nums, 0, (int)nums.size() - 1);
return nums;
}
void mergeSort(vector<int>& nums, int start, int end) {
if (start >= end) return;
int mid = (start + end) / 2;
mergeSort(nums, start, mid);
mergeSort(nums, mid + 1, end);
merge(nums, start, mid, end);
}
void merge(vector<int>& nums, int start, int mid, int end) {
vector<int> tmp(end - start + 1);
int i = start, j = mid + 1, k = 0;
while (i <= mid && j <= end) {
if (nums[i] < nums[j]) tmp[k++] = nums[i++];
else tmp[k++] = nums[j++];
}
while (i <= mid) tmp[k++] = nums[i++];
while (j <= end) tmp[k++] = nums[j++];
for (int idx = 0; idx < tmp.size(); ++idx) {
nums[idx + start] = tmp[idx];
}
}
};
Github 同步地址:
https://github.com/grandyang/leetcode/issues/912
类似题目:
Kth Largest Element in an Array
参考资料:
https://leetcode.com/problems/sort-an-array/
https://leetcode.com/problems/sort-an-array/discuss/319326/Java-merge-sort-implementation
https://leetcode.com/problems/sort-an-array/discuss/293820/Easiest-and-fastest-solution.-O(10n)
[LeetCode All in One 题目讲解汇总(持续更新中...)](https://www.cnblogs.com/grandyang/p/4606334.html)
相关文章
- Leetcode: Intersection of Two Arrays II
- Leetcode: Rotate Array
- LeetCode N-Queens
- [leetcode]Binary Tree Level Order Traversal
- LeetCode高频题34. 在排序数组中查找元素的第一个和最后一个位置
- LeetCode高频题33. 搜索旋转排序数组
- JS Leetcode 154. 寻找旋转排序数组中的最小值 II 题解分析
- JS leetcode 删除排序数组中的重复项 题解分析
- 团灭LeetCode跳跃游戏(相关话题:贪心,BFS)
- 62、【数组】leetcode——54. 螺旋矩阵:N*M型(C++版本)
- LeetCode 4. 寻找两个有序数组的中位数
- [LeetCode] 1191. K-Concatenation Maximum Sum K次串联后最大子数组之和
- [LeetCode] 1157. Online Majority Element In Subarray 子数组中占绝大多数的元素
- [LeetCode] 1122. Relative Sort Array 数组的相对排序
- [LeetCode] 996. Number of Squareful Arrays 平方数组的个数
- [LeetCode] 974. Subarray Sums Divisible by K 子数组数字之和可被K整除
- [LeetCode] 941. Valid Mountain Array 验证山形数组
- [LeetCode] 922. Sort Array By Parity II 按奇偶排序数组之二
- [LeetCode] 713. Subarray Product Less Than K 子数组乘积小于K
- [LeetCode] Second Minimum Node In a Binary Tree 二叉树中第二小的结点
- [LeetCode] Split Array with Equal Sum 分割数组成和相同的子数组
- [LeetCode] 545. Boundary of Binary Tree 二叉树的边界
- [LeetCode] 350. Intersection of Two Arrays II 两个数组相交之二
- [LeetCode] 269. Alien Dictionary 另类字典
- LeetCode将有序数组转换为二叉搜索树
- leetcode 88. Merge Sorted Array 归并两个有序数组
- leetcode 153. Find Minimum in Rotated Sorted Array 寻找旋转排序数组中的最小值(中)
- leetcode 81. Search in Rotated Sorted Array II 搜索旋转排序数组 II(中等)
- leetcode 215. Kth Largest Element in an Array 数组中的第K个最大元素