MCPcopy Create free account
hub / github.com/Seogeurim/CS-study / partition

Method partition

contents/algorithm/code/QuickSort.java:14–35  ·  view source on GitHub ↗
(int[] arr, int start, int end)

Source from the content-addressed store, hash-verified

12 }
13
14 static int partition(int[] arr, int start, int end) {
15 int low = start + 1; // pivot을 맨 왼쪽 값으로 할것이기 때문에 그 다음 값부터 확인
16 int high = end;
17 int pivot = arr[start]; // 가장 왼쪽 값을 pivot으로 설정
18
19 while (low <= high) { // 양쪽에서 탐색하면서 둘이 겹쳐져 지나칠때 까지 한다.
20 while (low <= end && arr[low] < pivot) { // 앞에서 부터 비교중 pivot 보다 크면 stop
21 low++;
22 }
23 while (high >= start && arr[high] > pivot) { // 뒤에서 부터 비교중 pivot 보다 작으면 stop
24 high--;
25 }
26 if (low < high) { // low , high 가 겹쳐져 지나친게 아니면 둘을 바꿔줌
27 swap(arr, low, high);
28 }
29 }
30 swap(arr, start, high); // 마지막으로 pivot과 high index의 값을 바꾸면 high index 가 pivot의 index가 됨
31
32 System.out.println(Arrays.toString(arr) + " pivot: " + pivot + " result index: " + high);
33
34 return high; // pivot 위치 반환
35 }
36
37 static void swap(int[] arr, int i, int j) {
38 int temp = arr[j];

Callers 1

quickSortMethod · 0.95

Calls 2

swapMethod · 0.95
toStringMethod · 0.45

Tested by

no test coverage detected