-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort_Random.java
More file actions
59 lines (46 loc) · 1.34 KB
/
Copy pathQuickSort_Random.java
File metadata and controls
59 lines (46 loc) · 1.34 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
package AlogrithmCourse;
import java.util.Random;
public class QuickSort_Random {
public static Random random=new Random();
public static void QuickSort(int[] array,int low,int high) {
if(low<high) {
int pivot=RandomizedPartition(array, low, high);
QuickSort(array, low, pivot-1);
QuickSort(array, pivot+1, high);
}
}
public static int RandomizedPartition(int[] array,int low,int high) {
int randInt=random.nextInt(high)%(high-low+1)+low;
int temp=array[randInt];
array[randInt]=array[high];
array[high]=temp;
return Partition(array,low,high);
}
public static int Partition(int[] array,int low,int high) {
int middle=array[high];
int i=low-1;
for(int j=low;j<=high-1;j++) {
if(array[j]<=middle) {
i+=1;
int temp=array[i];
array[i]=array[j];
array[j]=temp;
}
}
int temp=array[i+1];
array[i+1]=array[high];
array[high]=temp;
return i+1;
}
public static void main(String[] args) {
int bound=new Random().nextInt(1000000-100000)+100000;
int[] array=new int[bound];
for(int i=0;i<bound;i++) {
array[i]=new Random().nextInt(bound);
}
long startTime=System.currentTimeMillis();
QuickSort(array, 0, array.length-1);
long endTime=System.currentTimeMillis();
System.out.println(endTime-startTime+"ms");
}
}