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)

[LeetCode] 912. Sort an Array 数组排序的更多相关文章

  1. Leetcode 912. Sort an Array

    class Solution: def sortArray(self, nums: List[int]) -> List[int]: return sorted(nums)

  2. 【LeetCode】912. Sort an Array 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 库函数排序 桶排序 红黑树排序 归并排序 快速排序 ...

  3. 【leetcode】912. Sort an Array

    题目如下: Given an array of integers nums, sort the array in ascending order. Example 1: Input: [5,2,3,1 ...

  4. LeetCode 360. Sort Transformed Array

    原题链接在这里:https://leetcode.com/problems/sort-transformed-array/description/ 题目: Given a sorted array o ...

  5. [LeetCode] 360. Sort Transformed Array 排序转换后的数组

    Given a sorted array of integers nums and integer values a, b and c. Apply a function of the form f( ...

  6. LeetCode 912. 排序数组(Sort an Array) 43

    912. 排序数组 912. Sort an Array 题目描述 每日一算法2019/6/15Day 43LeetCode912. Sort an Array

  7. [CareerCup] 11.2 Sort Anagrams Array 异位词数组排序

    11.2 Write a method to sort an array of strings so that all the anagrams are next to each other. 这道题 ...

  8. #Leetcode# 922. Sort Array By Parity II

    https://leetcode.com/problems/sort-array-by-parity-ii/ Given an array A of non-negative integers, ha ...

  9. [LeetCode] 922. Sort Array By Parity II 按奇偶排序数组之二

    Given an array A of non-negative integers, half of the integers in A are odd, and half of the intege ...

随机推荐

  1. 大话设计模式Python实现-观察者模式

    观察者模式(发布-订阅模式 Publish Subscribe Pattern):定义了一种一对多的关系,让多个观察对象同时监听一个主题对象,当主题对象状态发生变化时会通知所有观察者,是它们能够自动更 ...

  2. 基于kafka_2.11-2.1.0实现的生产者和消费者代码样例

    1.搭建部署好zookeeper集群和kafka集群,这里省略. 启动zk: bin/zkServer.sh start conf/zoo.cfg. 验证zk是否启动成功: bin/zkServer. ...

  3. webapi 导入excel处理数据

    参考资料     https://blog.csdn.net/pan_junbiao/article/details/82935992 https://www.cnblogs.com/dansedia ...

  4. 编译 datax

    datax 是阿里巴巴官方开源的一个数据同步工具,可以用于诸多数据源之间的同步,并且使用简单.效率高. datax 官方有提供编译好的版本,可以直接下载,但是其中包含有 BUG. 我最近遇到的一个问题 ...

  5. C#写日志工具类

    代码: using System; using System.Collections.Generic; using System.IO; using System.Linq; using System ...

  6. Java操作zip-压缩和解压文件

    一.说明 rar格式的压缩包收费,java支持zip格式的压缩和解压 二.工具类 import java.io.*; import java.util.Enumeration; import java ...

  7. CMake编译的VS工程,安装时遇到错误:error MSB3073: 命令“setlocal

    错误提示 70>CMake Error at src/base/cmake_install.cmake:63 (file): 70> file INSTALL cannot find 70 ...

  8. 拥抱小程序,WeTest小程序全链路测试解决方案正式上线

    背景 随着微信开放小程序开发功能,迅速在各个实体店抢占流量入口,广大商家看到了在线和离线的机会整合,利用小程序版本特点低成本进入市场,达到流量的获取和转化. 伴随着资本的进入,小程序开发市场也因此越来 ...

  9. react-custom-scrollbars的使用

    react-custom-scrollbars的作用 流畅的本机浏览器滚动 移动设备的本机滚动条 完全可定制 自动隐藏 自动高度 通用(在客户端和服务器上运行) requestAnimationFra ...

  10. 机器学习之sigmoid函数

      先说一下,ML小白. 这是第一次写个人博客类似东西, 主要来说说看 sigmoid 函数,sigmoid函数是机器学习中的一个比较常用的函数,与之类似的还有softplus和softmax等函数, ...