Since the core is selected from a single array and because of how the logic of the algorithm is implemented, you never need to check that the indices go out of bounds. At some point, the conditions must be true.
The correctness of the algorithm can be proved using cycle invariants.
1. private int partition(int from, int to)
2. {
3. int pivot = a[from];
4. int i = from - 1;
5. int j = to + 1;
6. while (i < j)
7. {
8. i++;
9.
10. while (a[i] < pivot) i++;
11. j--;
12.
13. while (a[j] > pivot) j--;
14. if (i < j) swap(i, j);
15.
16.
17. }
18. return j;
19. }
9 12 ( 15, 16) , 6 17. , i j .
9, 12 .
1- , a[from] i = from.
( 1- ) i, j. i < j, 15. i 8 9 , 15. , 9 6 17.
pivot , [to], . .
sort(from, p == to ? p - 1 : p);
sort(p + 1, to);
sort(from, p);
sort(p + 1, to);