-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquickSortDemo.java
More file actions
95 lines (88 loc) · 2.61 KB
/
quickSortDemo.java
File metadata and controls
95 lines (88 loc) · 2.61 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package quickSortDemo;
import java.util.Scanner;
public class quickSortDemo {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
int n = in.nextInt();
int[] nums = new int[n];
for (int i = 0; i < nums.length; i++) {
nums[i] = in.nextInt();
}
quickSort(nums, false);
// Arrays.sort(nums);
for (int i = 0; i < nums.length; i++) {
if ((i + 1) % 100 == 0 || i == nums.length - 1) {
System.out.println(nums[i]);
} else {
System.out.print(nums[i] + " ");
}
}
}
in.close();
}
public static void quickSort(int[] nums) {
quickSort(0, nums.length - 1, nums, true);
}
public static void quickSort(int[] nums, boolean lowToHigh) {
quickSort(0, nums.length - 1, nums, lowToHigh);
}
public static void quickSort(
int left,
int right,
int[] nums,
boolean lowToHigh
) {
int l = left;
int r = right;
int p = nums[(left + right) / 2];
while (l <= r) {
while ((p > nums[l] && lowToHigh) || (p < nums[l] && !lowToHigh)) {
l++;
}
while ((p < nums[r] && lowToHigh) || (p > nums[r] && !lowToHigh)) {
r--;
}
if (l < r) {
int temp = nums[r];
nums[r] = nums[l];
nums[l] = temp;
l++;
r--;
} else if (l == r) {
l++;
}
}
if (left < r) {
quickSort(left, r, nums, lowToHigh);
}
if (l < right) {
quickSort(l, right, nums, lowToHigh);
}
}
public static void quickSortF(int top, int tail, int[] nums) {
if (top >= tail) return;
int mid = nums[tail];
int left = top, right = tail - 1;
while (left < right) {
while (nums[left] < mid && left < right) {
left++;
}
while (nums[right] >= mid && left < right) {
right--;
}
int temp = nums[left];
nums[left] = nums[right];
nums[right] = temp;
}
if (nums[left] >= nums[tail]) {
int temp = nums[left];
nums[left] = nums[tail];
nums[tail] = temp;
} else {
left++;
}
quickSortF(top, left - 1, nums);
quickSortF(left + 1, tail, nums);
}
}