-
Notifications
You must be signed in to change notification settings - Fork 0
/
QuickSort.java
executable file
·72 lines (62 loc) · 1.31 KB
/
QuickSort.java
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
import java.util.Random;
public class QuickSort {
public int[] mData;
private int mSize;
public QuickSort(int size){
mData = new int[size];
mSize = size;
generateData();
}
public static void main(String[] args){
int testSize = 1000;
QuickSort qsort = new QuickSort(testSize);
int[] data = qsort.getData();
for (int i = 0;i < data.length ;i++ ) {
System.out.print(":"+data[i]);
}
System.out.println("Start");
qsort.QuickSortRecursive(0, testSize-1);
int[] result = qsort.getData();
for (int i = 0;i < result.length ;i++ ) {
System.out.print(":"+result[i]) ;
}
System.out.println("complete");
}
public void generateData(){
Random rand = new Random();
for(int i=0;i<mSize;i++){
mData[i] = rand.nextInt(100);
}
}
public int[] getData(){
return mData;
}
public void QuickSortRecursive(int Left, int Right){
int pivot = Left;
int i = Left;
int j = Right;
while(true){
while(mData[pivot]>=mData[i] && i < Right){
i++;
}
while(mData[pivot]<=mData[j] && j > Left){
j--;
}
if(i>=j){
break;
}else{
swap(i, j);
}
}
swap(pivot, j);
if(Left<j)
QuickSortRecursive(Left, j);
if(Right>i)
QuickSortRecursive(i, Right);
}
public void swap(int x, int y){
int temp = mData[x];
mData[x] = mData[y];
mData[y] = temp;
}
}