-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.java
More file actions
90 lines (79 loc) · 2.22 KB
/
QuickSort.java
File metadata and controls
90 lines (79 loc) · 2.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
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package com.yijie.sort;
import java.util.Arrays;
public class QuickSort {
public static void main(String[] args) {
int[] arr = {-9, 78, 0, 23, -567, 70, 0, -9, 500, 70, -9};
quickSort2(arr, 0, arr.length - 1);
System.out.println("after quick sort: " + Arrays.toString(arr));
}
public static void quickSort(int[] arr, int left, int right) {
int l = left;//left index
int r = right;//right index
//pivot
int pivot = arr[(left + right) / 2];
int temp = 0; //auxiliary variable
while (l < r) {
//searching to the left of pivot, if an element grater than pivot was found, quit
while (arr[l] < pivot) {
l += 1;
}
while (arr[r] > pivot) {
r -= 1;
}
if (l >= r) {
break;
}
//swap the left and right
temp = arr[l];
arr[l] = arr[r];
arr[r] = temp;
//if arr[l] == pivot, r--
if (arr[l] == pivot) {
r--;
}
//if arr[r] == pivot, l++
if (arr[r] == pivot) {
l++;
}
//if l == r, l++, r--
if (l == r) {
l++;
r--;
}
//recursion to the left
if (left < r) {
quickSort(arr, left, r);
}
//recursion to the left
if (right > l) {
quickSort(arr, l, right);
}
}
}
public static void quickSort2(int[] arr, int left, int right) {
int l = left;
int r = right;
int pivot = arr[(left + right) / 2];
do {
while (arr[l] < pivot) {
l++;
}
while (arr[r] > pivot) {
r--;
}
if (l <= r) {
int temp = arr[l];
arr[l] = arr[r];
arr[r] = temp;
l++;
r--;
}
} while (l <= r);
if (left < r) {
quickSort2(arr, left, r);
}
if (l < right) {
quickSort2(arr, l, right);
}
}
}