forked from damaohongtu/JavaInterview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.java
More file actions
46 lines (44 loc) · 1.1 KB
/
Copy pathQuickSort.java
File metadata and controls
46 lines (44 loc) · 1.1 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
package ALiBaBa;
import org.junit.Test;
/**
* @Author MaoTian
* @Classname QuickSort
* @Description TODO
* @Date 上午11:39 2019/8/17
* @Version 1.0
* @Created by mao<[email protected]>
*/
public class QuickSort {
public void quickSort(int[] nums,int left,int right){
if(left<right){
int mid=partition(nums,left,right);
quickSort(nums,left,mid-1);
quickSort(nums,mid+1,right);
}
}
public int partition(int[] nums,int left,int right){
int pos=left;
int value=nums[pos];
for (int i=left;i<=right;i++){
if(nums[i]<value){
pos++;
if(pos!=i){
int tmp=nums[pos];
nums[pos]=nums[i];
nums[i]=tmp;
}
}
}
nums[left]=nums[pos];
nums[pos]=value;
return pos;
}
@Test
public void check(){
int[] nums={2,4,2,6,7,3,2,6,8};
quickSort(nums,0,nums.length-1);
for (int i=0;i<nums.length;i++){
System.out.println(nums[i]);
}
}
}