# 排序数组
- 排序数组
来源:力扣(LeetCode) 链接 (opens new window):https://leetcode.cn/problems/sort-an-array/ 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
# 问题
给你一个整数数组 nums,请你将该数组升序排列。
输入:nums = [5,2,3,1]
输出:[1,2,3,5]
# 思路
快速排序
//快速排序
var sortArray = function (nums) {
function pivot(nums, start, end) {
let pivot = nums[start];
let left = start;
let right = end;
while (left < right) {
//右指针左移
while (left < right && nums[right] > pivot) {
right--;
}
//左指针左移
while (left < right && nums[left] <= pivot) {
left++;
}
if (left < right) {
let temp = nums[left];
nums[left] = nums[right];
nums[right] = temp;
}
}
let temp = nums[left];
nums[left] = nums[start];
nums[start] = temp;
return left;
}
function sort(nums, start, end) {
if (start >= end) {
return;
}
let middle = pivot(nums, start, end);
sort(nums, start, middle - 1);
sort(nums, middle + 1, end);
}
sort(nums, 0, nums.length - 1);
return nums;
};
冒泡排序
var sortArray = function (nums) {
for (let i = 0; i < nums.length; i++) {
for (let j = 0; j < nums.length - i; j++) {
if (nums[j] > nums[j + 1]) {
let temp = nums[j];
nums[j] = nums[j + 1];
nums[j + 1] = temp;
}
}
}
return nums;
};