-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/main' into main
- Loading branch information
Showing
2 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
|
||
|