-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSortBase.java
More file actions
48 lines (41 loc) · 1.31 KB
/
QuickSortBase.java
File metadata and controls
48 lines (41 loc) · 1.31 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
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class QuickSortBase {
private static Random r = new Random(System.currentTimeMillis());
protected static int partition(Integer[] arr, int pivot, int s, int e) {
swap(arr, pivot, s);
int curPos, lastSmall;
for (curPos = s + 1, lastSmall = s; curPos < e; curPos++) {
if (arr[curPos] <= arr[s]) {
if(lastSmall + 1 < curPos)
swap(arr, lastSmall + 1, curPos);
lastSmall++;
}// 4 2 3
}
swap(arr, s, lastSmall);
return lastSmall;
}
protected static void swap(Integer[] arr, int i, int j) {
Integer tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
protected static int randomPivot(Integer[] arr, int s, int e) {
return r.nextInt(e - s) + s;
}
protected static void print(Integer[] array) {
for (int i : array) {
System.out.print(i + " ");
}
System.out.println("");
}
protected static Integer[] readArray() {
Scanner s = new Scanner(System.in);
ArrayList<Integer> buf = new ArrayList();
while (s.hasNextInt()) {
buf.add(s.nextInt());
}
return buf.toArray(new Integer[0]);
}
}