912:排序数组

This commit is contained in:
huangge1199@hotmail.com 2021-07-13 23:41:37 +08:00
parent 3696023436
commit 9aa79c1ff6
3 changed files with 114 additions and 1 deletions

View File

@ -0,0 +1,85 @@
//给你一个整数数组 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
//
// Related Topics 数组 分治 桶排序 计数排序 基数排序 排序 优先队列 归并排序
// 👍 315 👎 0
package leetcode.editor.cn;
import java.util.Random;
//912:排序数组
class SortAnArray {
public static void main(String[] args) {
//测试代码
Solution solution = new SortAnArray().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
private Random random;
public int[] sortArray(int[] nums) {
random=new Random(System.currentTimeMillis());
pivot(nums, 0, nums.length - 1);
return nums;
}
private void pivot(int[] nums, int start, int end) {
if (start >= end)
return;
int mid = random.nextInt(end - start + 1) + start;
swap(nums, start, mid);
int num = nums[start];
int left = start + 1;
int right = end;
while (left < right) {
while (left < right && nums[left] <= num)
left++;
while (left < right && nums[right] >= num)
right--;
swap(nums, left, right);
}
if (nums[left] <= num)
swap(nums, left, start);
else {
swap(nums, left - 1, start);
left--;
}
pivot(nums, left + 1, end);
pivot(nums, start, left - 1);
}
private void swap(int[] nums, int start, int end) {
int temp = nums[start];
nums[start] = nums[end];
nums[end] = temp;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,28 @@
<p>给你一个整数数组&nbsp;<code>nums</code>,请你将该数组升序排列。</p>
<p>&nbsp;</p>
<ol>
</ol>
<p><strong>示例 1</strong></p>
<pre><strong>输入:</strong>nums = [5,2,3,1]
<strong>输出:</strong>[1,2,3,5]
</pre>
<p><strong>示例 2</strong></p>
<pre><strong>输入:</strong>nums = [5,1,1,2,0,0]
<strong>输出:</strong>[0,0,1,1,2,5]
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ol>
<li><code>1 &lt;= nums.length &lt;= 50000</code></li>
<li><code>-50000 &lt;= nums[i] &lt;= 50000</code></li>
</ol>
<div><div>Related Topics</div><div><li>数组</li><li>分治</li><li>桶排序</li><li>计数排序</li><li>基数排序</li><li>排序</li><li>堆(优先队列)</li><li>归并排序</li></div></div>\n<div><li>👍 315</li><li>👎 0</li></div>

File diff suppressed because one or more lines are too long