-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathQuickSort.java
More file actions
36 lines (32 loc) · 1.22 KB
/
QuickSort.java
File metadata and controls
36 lines (32 loc) · 1.22 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
package com.leecode.Sort;
import java.util.Arrays;
public class QuickSort {
public static void quickSort(int[] arr,int low,int high){
if(low<high){
int index=getBaseInt(arr,low,high);
quickSort(arr,0,index-1);
quickSort(arr,index+1,high);
}
}
public static int getBaseInt(int[] arr,int low,int high){
//基准数据
int tmp=arr[low];
while (low<high){
//如果从后往前,arr[high]小于基准数的话,high--,因为最后结果是 基准数左边都要小于它,基准数右边都要大于它
while (low<high && arr[high]>=tmp) high--;
//出现arr[high]大于基准数时需要将其赋值给low
arr[low]=arr[high];
while(low<high && arr[low]<=tmp) low++;
arr[high]=arr[low];
}
arr[low]=tmp;
return low;
}
public static void main(String[] args) {
// int[] arr = { 1, 2, 0, -97, 23, 22, 76, 1, 5, 8, 2, 0, -1, 22 };
int[] arr = { 1, 2, 0, -97, 23};
quickSort(arr, 0, arr.length - 1);
System.out.println("排序后:");
System.out.println(Arrays.toString(arr));
}
}