diff --git a/src/com/gatsby/_912SortAnArray.java b/src/com/gatsby/_912SortAnArray.java new file mode 100644 index 0000000..8e0da9a --- /dev/null +++ b/src/com/gatsby/_912SortAnArray.java @@ -0,0 +1,20 @@ +package com.gatsby; + +import com.gatsby.utils.SortUtil; + +/** + * @ClassName: _912SortAnArray + * @Description: leetcode 912 排序数组 + * @author: Gatsby + * @date: 2022/7/25 15:23 + */ + +public class _912SortAnArray { + public int[] sortArray(int[] nums) { + SortUtil.quickSort(nums, 0, nums.length - 1); + return nums; + } +} + + + diff --git a/src/com/gatsby/utils/SortUtil.java b/src/com/gatsby/utils/SortUtil.java new file mode 100644 index 0000000..1547ccf --- /dev/null +++ b/src/com/gatsby/utils/SortUtil.java @@ -0,0 +1,48 @@ +package com.gatsby.utils; + +/** + * @ClassName: SortUtil + * @Description: 排序类 + * @author: Gatsby + * @date: 2022/7/25 15:03 + */ + +public class SortUtil { + /** + * @MethodName: quickSort + * @Parameter: [nums, begin, end] + * @Return void + * @Description: 快速排序 + * @author: Gatsby + * @date: 2022/7/25 15:20 + */ + public static void quickSort(int[] nums, int begin, int end) { + if (begin < end) { + int pivot = nums[begin]; + int i = begin; + int j = end; + while (i < j) { + while (i < j && nums[j] > pivot) { + j--; + } + if (i < j) { + nums[i] = nums[j]; + i++; + } + + while (i < j && nums[i] < pivot) { + i++; + } + if (i < j) { + nums[j] = nums[i]; + j--; + } + } + nums[j] = pivot; + quickSort(nums, begin, j - 1); + quickSort(nums, j + 1, end); + } + } +} + +