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. 1 <= A.length <= 10000
  2. -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

Sort Colors

Count of Range Sum

参考资料:

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)

https://leetcode.com/problems/sort-an-array/discuss/280903/C%2B%2B-QuickSort-and-CountingSort-solutions

[LeetCode All in One 题目讲解汇总(持续更新中...)](https://www.cnblogs.com/grandyang/p/4606334.html)

最新文章

  1. git clone Linux 源码并切换TAG
  2. codeforces 744C Hongcow Buys a Deck of Cards
  3. AC日记——紧急措施 openjudge 1.7 22
  4. jqMobile中pageinit,pagecreate,pageshow等函数的执行顺序
  5. c++表达式的一些小小的注意事项
  6. 《Maven_孔浩》Maven依赖
  7. apache开源项目--Ignite
  8. iOS之Sqlite和FMDB
  9. HDU 5119 Happy Matt Friends(dp+位运算)
  10. CodeForces Round #191 (327C) - Magic Five 等比数列求和的快速幂取模
  11. Unity项目优化--开发项目的小经验
  12. 小程序解释HTML富文本的两种办法
  13. shell入门之函数应用
  14. Linux 安装 tomcat
  15. LeetCode算法题-Power of Four(Java实现-六种解法)
  16. Kindle:自动追更之云上之旅
  17. 第四百零一节,Django+Xadmin打造上线标准的在线教育平台—生产环境部署virtualenv虚拟环境安装,与Python虚拟环境批量安装模块
  18. PowerDesigner 16PDM显示备注
  19. ORM模板层
  20. 荣耀 6 安装 SD 卡,提示:SD卡已安全移除

热门文章

  1. 关于使用IDEA,使用Maven打包项目
  2. 你需要知道的8个CSS带@的规则
  3. Preface_英语
  4. 【UOJ#76】【UR #6】懒癌(动态规划)
  5. SQL Server温故系列(0):导航目录
  6. 微信接口调用&#39;updateTimelineShareData&#39;,&#39;updateAppMessageShareData&#39; 的踩坑记录
  7. 深入浅出JVM的锁优化案例
  8. 在 Docker 中已运行的 container 如何修改 run 时的 env
  9. 带你理解Xcode Derived Data
  10. Struts2 Action的3种创建方式