BFPRT recursive method to find the k-th smallest element. @param arr the input array @param begin the starting index @param end the ending index @param i the index of the desired smallest element @return the k-th smallest element
(int[] arr, int begin, int end, int i)
| 69 | * @return the k-th smallest element |
| 70 | */ |
| 71 | public static int bfprt(int[] arr, int begin, int end, int i) { |
| 72 | if (begin == end) { |
| 73 | return arr[begin]; |
| 74 | } |
| 75 | int pivot = medianOfMedians(arr, begin, end); |
| 76 | int[] pivotRange = partition(arr, begin, end, pivot); |
| 77 | if (i >= pivotRange[0] && i <= pivotRange[1]) { |
| 78 | return arr[i]; |
| 79 | } else if (i < pivotRange[0]) { |
| 80 | return bfprt(arr, begin, pivotRange[0] - 1, i); |
| 81 | } else { |
| 82 | return bfprt(arr, pivotRange[1] + 1, end, i); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Finds the median of medians as the pivot element. |
no test coverage detected