快排模板

 1 import java.io.BufferedInputStream;
 2 import java.util.Scanner;
 3 
 4 public class Main{
 5     public static void main(String[] args) {
 6         Scanner scan = new Scanner(new BufferedInputStream(System.in));
 7         int n = scan.nextInt();
 8         int[] arr = new int[n];
 9         for(int i =  0; i < n; i++) {
10             arr[i] = scan.nextInt();
11         }
12         method(arr,0,n - 1);
13         for(int i = 0; i < n; i++) {
14             System.out.print(arr[i] + " ");
15         }
16     }
17     public static void method(int[] arr,int l,int r) {
18         if(l >= r) return;
19         int i = l - 1, j = r + 1, x = arr[(l + r) / 2];
20         while(i < j) {
21             do {
22                 i++;
23             }while(arr[i] < x);
24             do {
25                 j--;
26             }while(arr[j] > x);
27             if(i < j) {
28                 int temp = arr[i];
29                 arr[i] = arr[j];
30                 arr[j] = temp;
31             }
32         }
33         method(arr, l, j);
34         method(arr, j + 1, r);
35     }
36 }

 

上一篇:JavaSE基础--用户转换 Scanner


下一篇:Redis:scan命令