-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.java
More file actions
54 lines (46 loc) · 1.22 KB
/
QuickSort.java
File metadata and controls
54 lines (46 loc) · 1.22 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
// AUTHOR: Soel Micheletti
import java.util.Random;
class QuickSort{
public static int[] quickSort(int[] a){
return quickSort(a, 0, a.length - 1);
}
public static int[] quickSort(int[] a, int left, int right){
int l = left;
int r = right;
int p = a[(left + right) / 2];
while(l <= r){
while(a[l]<p)
l++;
while(a[r]>p)
r--;
if(l <= r){
int tmp = a[l];
a[l] = a[r];
a[r] = tmp;
l++;
r--;
}
}
if(left < r)
quickSort(a, left, r);
if(l < right)
quickSort(a, l, right);
return a;
}
public static boolean isSorted(int[] a){
for(int i = 0; i < a.length - 1; i++){
if(a[i] > a[i + 1])
return false;
}
return true;
}
public static void main(String[] args) {
Random ran = new Random();
int[] a = new int[10000];
for(int i = 0; i < a.length; i++){
a[i] = ran.nextInt(10000);
}
quickSort(a);
System.out.println(isSorted(a));
}
}