-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuckSort.java
More file actions
45 lines (40 loc) · 1.21 KB
/
QuckSort.java
File metadata and controls
45 lines (40 loc) · 1.21 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
package com.company.algorithm;
import java.util.Arrays;
public class QuckSort {
public static void main(String[] args) {
int t[] = {10,8,6,5,9};
qucksort(t,0,4);
System.out.println(Arrays.toString(t));
}
/**
* 快速排序
* @param a 待排序的数组
* @param l 数组的左边界(例如,从起始位置开始排序,则l=0)
* @param r 数组的右边界(例如,排序截至到数组末尾,则r=a.length-1)
*/
public static void qucksort(int a[], int l, int r){
if (l < r){
int i,j,x;
i = l;
j = r;
x = a[i];
while (i < j){
while (i < j && a[j] > x){
j--;// 从右向左找第一个小于x的数
}
if (i < j){
a[i++] = a[j];
}
while (i < j && a[i] < x){
i++;// 从左向右找第一个大于x的数
}
if (i < j){
a[j--] = a[i];
}
}
a[i] = x;
qucksort(a,l,i - 1); /* 递归调用 */
qucksort(a,i + 1,r); /* 递归调用 */
}
}
}