标签:-- pack 接下来 image 结束 system style 处理 i++
1 package day08;
2
3
4
5 public class QuickSort {
6
7 public static void quickSort(int[] arr, int left, int right) {
8
9 int i, j, temp, t;
10
11 if (left > right) {
12
13 return;
14
15 }
16
17 i = left;
18
19 j = right;
20
21 //temp是基准位
22
23 temp = arr[left];
24
25 while (i < j) {
26
27 //j从右往左依次挪动进行递减
28
29 while (temp <= arr[j] && i < j) {
30
31 j--;
32
33 }
34
35 //i从左往右依次挪动进行递增
36
37 while (temp >= arr[i] && i < j) {
38
39 i++;
40
41 }
42
43 //如果满足条件进行交换
44
45 if (i < j) {
46
47 t = arr[j];
48
49 arr[j] = arr[i];
50
51 arr[i] = t;
52
53 }
54
55
56
57 }
58
59 //最后将基准位与i和j相等位置的数字交换
60
61 arr[left] = arr[i];
62
63 arr[i] = temp;
64
65 quickSort(arr, left, j - 1);
66
67 quickSort(arr, j + 1, right);
68
69 }
70
71
72
73 public static void main(String[] args) {
74
75 int[] arr = {6, 1, 2, 7, 9, 3, 4, 5, 10, 8};
76
77 quickSort(arr, 0, arr.length - 1);
78
79 for (int i = 0; i < arr.length; i++) {
80
81 System.out.print(arr[i] + "\t");
82
83 }
84
85 }
86
87 }
运行结果:
标签:-- pack 接下来 image 结束 system style 处理 i++
原文地址:https://www.cnblogs.com/xxeleanor/p/14405832.html