其他分享
首页 > 其他分享> > 快速排序模版

快速排序模版

作者:互联网

 1 import java.util.Scanner;
 2 
 3 public class QuickSort {
 4     static void quickSort(int q[], int l, int r){
 5         if (l >= r) return;
 6         int x = q[l], i = l - 1, j = r + 1;
 7         while (i < j){
 8             do i++; while (q[i] < x);
 9             do j--; while (q[j] > x);
10             if (i < j){
11                 int t = q[i];
12                 q[i] = q[j];
13                 q[j] = t;
14             }
15         }
16         quickSort(q, l, j);
17         quickSort(q, j+1, r);
18     }
19 
20     public static void main(String[] args) {
21         Scanner sc = new Scanner(System.in);
22         int num = sc.nextInt();
23         int[] q = new int[num];
24         for (int i = 0; i < num; i++) q[i] = sc.nextInt();
25         quickSort(q, 0, num-1);
26         for (int i = 0; i < num; i++) System.out.print(q[i] + " ");
27     }
28 }

 

标签:Scanner,int,模版,quickSort,++,while,num,排序,快速
来源: https://www.cnblogs.com/csw2021/p/16410764.html