forked from hansiming/JavaProject
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.java
More file actions
69 lines (54 loc) · 1.06 KB
/
QuickSort.java
File metadata and controls
69 lines (54 loc) · 1.06 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
60
61
62
63
64
65
66
67
68
69
package com.csdhsm.sort;
/**
* @Title: QuickSort.java
* @Package: com.csdhsm.sort
* @Description 快速排序
* @author Han
* @date 2016-4-3 上午11:37:02
* @version V1.0
*/
public class QuickSort {
/**
* 快速排序递归调用
* @Description
* @author Han
* @param arr
* @param low
* @param high
*/
public void sort(int[] arr,int low,int high){
if(low < high){
int pos = findPoss(arr,low,high);
sort(arr,low,pos-1);
sort(arr,pos+1,high);
}
}
/**
* @Description 寻找合适的位置
* @author Han
* @param a
* @param low
* @param high
*/
public int findPoss(int arr[],int low,int high){
/**
* t为锚点,左边都是小于t的数字,右边都是大于t的数字
*/
int t = arr[low];
/**
* 一直要找到low等于high为止
*/
while(low < high){
while(low < high && arr[high] >= t){
high--;
}
arr[low] = arr[high];
while(low <high && arr[low] <= t){
low++;
}
arr[high] = arr[low];
}
arr[low] = t;
return low;
}
}