Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
BGMer7 committed Jul 28, 2022
2 parents 9645da9 + 783755f commit 884e86f
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/com/gatsby/_912SortAnArray.java
Original file line number Diff line number Diff line change
@@ -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;
}
}



48 changes: 48 additions & 0 deletions src/com/gatsby/utils/SortUtil.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
}


0 comments on commit 884e86f

Please sign in to comment.