当前位置 : 首页 » 文章分类 :  算法  »  LeetCode.912.Sort an Array 排序数组

LeetCode.912.Sort an Array 排序数组

题目描述

912 Sort an Array
https://leetcode-cn.com/problems/sort-an-array/

Given an array of integers nums, sort the array in ascending order.

Example 1:

  1. Input: nums = [5,2,3,1]
  2. Output: [1,2,3,5]

Example 2:

  1. Input: nums = [5,1,1,2,0,0]
  2. Output: [0,0,1,1,2,5]

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


解题过程

这道题的目的应该是让系统的复习排序算法。

快速排序

写了下快速排序,实现了3中枢轴选择策略:
1、每次选择最左边的
2、随机
3、left, mid ,right 三者选中

根据提交OJ运行的结果,还是最简单的选最左边第一个元素做枢轴速度最快。

快速排序平均时间复杂度 O(nlogn),空间复杂度 log(n),不稳定

  1. private static class SolutionV2020 {
  2. public int[] sortArray(int[] nums) {
  3. if (null == nums || nums.length < 2) {
  4. return nums;
  5. }
  6. qsort(nums, 0, nums.length - 1);
  7. return nums;
  8. }
  9. private void qsort(int[] nums, int left, int right) {
  10. if (left >= right) {
  11. return;
  12. }
  13. int location = partition(nums, left, right);
  14. qsort(nums, left, location - 1);
  15. qsort(nums, location + 1, right);
  16. }
  17. private int partition(int[] nums, int left, int right) {
  18. // 从nums[left,...,right]中随机找一个数放到 left 下标的位置
  19. // randomFirst(nums, left, right);
  20. // 找 left,mid,right 中间的数放到 left 下标的位置
  21. middleFirst(nums, left, right);
  22. int pivot = nums[left];
  23. while (left < right) {
  24. while (left < right && nums[right] >= pivot) {
  25. right--;
  26. }
  27. nums[left] = nums[right];
  28. while (left < right && nums[left] <= pivot) {
  29. left++;
  30. }
  31. nums[right] = nums[left];
  32. }
  33. nums[left] = pivot;
  34. return left;
  35. }
  36. // 从nums[left,...,right]中随机找一个数放到 left 下标的位置
  37. private void randomFirst(int[] nums, int left, int right) {
  38. int randomIndex = left + (int)(Math.random() * (right - left));
  39. int temp = nums[randomIndex];
  40. nums[randomIndex] = nums[left];
  41. nums[left] = temp;
  42. }
  43. // 找 left,mid,right 中间的数放到 left 下标的位置
  44. private void middleFirst(int[] nums, int left, int right) {
  45. if (right - left < 2) { // 小于3个元素
  46. return;
  47. }
  48. int mid = (left + right) / 2;
  49. int min = Math.min(nums[left], Math.min(nums[mid], nums[right]));
  50. int max = Math.max(nums[left], Math.max(nums[mid], nums[right]));
  51. if (nums[left] != max && nums[left] != min) {
  52. // nums[left] 就是中间元素
  53. return;
  54. }
  55. int midIndex; // 中间值的下标
  56. if (nums[left] == min) {
  57. midIndex = nums[mid] < nums[right] ? mid : right;
  58. } else { // nums[left]==max
  59. midIndex = nums[mid] > nums[right] ? mid : right;
  60. }
  61. int temp = nums[midIndex];
  62. nums[midIndex] = nums[left];
  63. nums[left] = temp;
  64. }
  65. }

GitHub代码

algorithms/leetcode/leetcode/_912_SortAnArray.java
https://github.com/masikkk/algorithms/blob/master/leetcode/leetcode/_912_SortAnArray.java


上一篇 LeetCode.1111.Maximum Nesting Depth of Two Valid Parentheses Strings 有效括号的嵌套深度

下一篇 LeetCode.剑指offer.062.圆圈中最后剩下的数字/约瑟夫环

阅读
评论
532
阅读预计2分钟
创建日期 2020-03-31
修改日期 2020-03-31
类别

页面信息

location:
protocol: http:
host: devgou.com
hostname: devgou.com
origin: http://devgou.com
pathname: /article/LeetCode.912.SortAnArray/
href: http://devgou.com/article/LeetCode.912.SortAnArray/
document:
referrer:
navigator:
platform: Linux x86_64
userAgent: Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)

评论