-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSortStudy.java
More file actions
45 lines (36 loc) · 1.17 KB
/
Copy pathQuickSortStudy.java
File metadata and controls
45 lines (36 loc) · 1.17 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
package algorithm.sort.nlognSort;// 퀵 정렬
import java.util.Arrays;
public class QuickSortStudy {
public static void quickSort(int[] arr, int left, int right) {
if (left >= right) {
return;
}
int pivot = partition(arr, left, right);// 반갈 후 중간값
quickSort(arr, left, pivot - 1);
quickSort(arr, pivot + 1, right);
}
public static int partition(int[] arr, int left, int right) {
int pivot = left++;
while (left <= right) {
while (left < arr.length && arr[left] < arr[pivot]) {
left++;
}
while (right > 0 && arr[right] > arr[pivot]) {
right--;
}
if (left < right) swap(arr, left, right);
}
swap(arr, pivot, right);
return right;
}
public static void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
public static void main(String[] args) {
int[] arr = {6, 2, 7, 9, 4, 5, 8};
quickSort(arr, 0, arr.length - 1);
System.out.println("퀵 정렬: " + Arrays.toString(arr));
}
}