Hoare 파티셔닝 알고리즘이 항상 배열을 두 개의 오른쪽 파트로 분할하는 이유를 알아 내려고합니다. 아래의 코드에서, 나는 Hoare algorithm
이 좀 더 명확 나에게 할빠른 정렬 Hoare 배열 파티셔닝
int partition(int[] arr, int leftIndex, int rightIndex) {
int pivot = arr[(leftIndex + rightIndex)/2];
while (leftIndex <= rightIndex) {
while (arr[leftIndex] < pivot) leftIndex++;
while (arr[rightIndex] > pivot) rightIndex--;
// If all numbers at right places, than leftIndex and rightIndex
// could point at same array element index
// So it's means partion done.
// We should return leftIndex + 1 cause
// rightIndex points at the last element of the left sub array
if (leftIndex == rightIndex) return leftIndex + 1;
if (leftIndex < rightIndex) {
swap(arr, leftIndex, rightIndex);
leftIndex++;
rightIndex--;
}
}
//But here the tricky thing: Why does this "if case" never execute?
if (leftIndex - 1 > rightIndex)
System.out.println("leftIndex - 1 > rightIndex");
return leftIndex;
}
그래서 질문입니다 (자세한 내용은 주석 참조) 확장 : 그것은 가능 기능을 분할하는 배열을 전달하기를, 그래서 라인은 아래 실행됩니다 ? 즉 실행하려면
if (leftIndex - 1 > rightIndex)
System.out.println("leftIndex - 1 > rightIndex");?