Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
73 lines (67 loc) · 2.5 KB

File metadata and controls

73 lines (67 loc) · 2.5 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package test;
import java.util.Arrays;
public class QuickSort {
public static void main(String[] args) {
int[] array = {6,72,113,11,23};
quickSort(array, 0, array.length -1);
System.out.println("排序后的结果");
System.out.println(Arrays.toString(array));
}
private static void quickSort(int[] array, int low, int high) {
if(low < high) {
int x = array[low];
int i = low;
int j = high;
while (i < j) {
while(i < j && array[j] > x) {
j--;
}
if(i < j) {
array[i++] = array[j];
}
while (i < j && array[i] < x) {
i++;
}
if(i < j) {
array[j--] = array[i];
}
}
array[i] = x;
quickSort(array, low, i);
quickSort(array, i + 1, high);
}
}
}
class QuickSort_1 {
public static void quickSort(int[] array, int low, int high) {
if(low < high) {
// 获取划分子数组的位置
int position = partition(array, low, high);
quickSort(array, low, position);
quickSort(array, position + 1, high);
}
}
private static int partition(int[] array, int low, int high) {
// 取最后一个元素作为中心元素
int pivot = array[high];
// 定义指向比中心元素大的指针,首先指向第一个元素
int pointer = low;
// 遍历数组中的所有元素,将比中心元素大的放右边,比元素小的放左边
for(int i = low; i < high; i++) {
if(array[i] <= pivot) {
// 将比中心元素小的元素和指针指向的元素交换位置
// 如果第一个元素比中心元素小,这里就是自己和自己交换位置,指针和索引都向下一位移动
// 如果元素比中心元素大,索引向下移动,指针指向这个较大的元素,直到找到比中心元素小的元素,并交换位置,指针向下移动
int temp = array[i];
array[i] = array[pointer];
array[pointer] = temp;
pointer++;
}
}
// 将中心元素和指针指向的元素交换位置
int temp = array[pointer];
array[pointer] = array[high];
array[high] = temp;
return pointer;
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.