-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSortAnArray.java
More file actions
37 lines (33 loc) · 972 Bytes
/
SortAnArray.java
File metadata and controls
37 lines (33 loc) · 972 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package com.dbc;
import java.util.Random;
public class SortAnArray {
public void quickSort(int left, int right, int[] nums) {
int point = (left + right) / 2;
swap(left, point, nums);
int mid = nums[left], l = left, r = right;
while (l < r) {
while (l < r && nums[r] >= mid) r--;
if (l < r) {
nums[l] = nums[r];
l++;
}
while (l < r && nums[l] <= mid) l++;
if (l < r) {
nums[r] = nums[l];
r--;
}
}
nums[l] = mid;
if (l - 1 > left) quickSort(left, l - 1, nums);
if (l + 1 < right) quickSort(l + 1, right, nums);
}
public void swap(int a, int b, int[] nums) {
int temp = nums[a];
nums[a] = nums[b];
nums[b] = temp;
}
public int[] sortArray(int[] nums) {
quickSort(0, nums.length - 1, nums);
return nums;
}
}