public
class
QSort
04 |
{ |
05 |
06 |
/** |
07 |
* @param args |
08 |
*/ |
09 |
public static void main(String[] args) |
10 |
{ |
11 |
// TODO 自動生成方法存根 |
12 |
quicksort qs = new quicksort(); |
13 |
int data[] = { 44 , 22 , 2 , 32 , 54 , 22 , 88 , 77 , 99 , 11 }; |
14 |
qs.data = data; |
15 |
qs.sort( 0 , qs.data.length- 1 ); |
16 |
qs.display(); |
17 |
} |
18 |
19 |
} |
20 |
21 |
22 |
class quicksort |
23 |
{ |
24 |
public int data[]; |
25 |
|
26 |
private int partition( int sortArray[], int low, int hight) |
27 |
{ |
28 |
int key = sortArray[low]; |
29 |
|
30 |
while (low<hight) |
31 |
{ |
32 |
while (low<hight && sortArray[hight]>=key) |
33 |
hight--; |
34 |
sortArray[low] = sortArray[hight]; |
35 |
|
36 |
while (low<hight && sortArray[low]<=key) |
37 |
low++; |
38 |
sortArray[hight] = sortArray[low]; |
39 |
} |
40 |
sortArray[low] = key; |
41 |
return low; |
42 |
} |
43 |
|
44 |
public void sort( int low, int hight) |
45 |
{ |
46 |
if (low<hight) |
47 |
{ |
48 |
int result = partition(data,low,hight); |
49 |
sort(low,result- 1 ); |
50 |
sort(result+ 1 ,hight); |
51 |
} |
52 |
|
53 |
} |
54 |
|
55 |
public void display() |
56 |
{ |
57 |
for ( int i= 0 ;i<data.length;i++) |
58 |
{ |
59 |
System.out.print(data[i]); |
60 |
System.out.print( " " ); |
61 |
} |
62 |
} |
63 |
} |