MCPcopy Create free account
hub / github.com/TheAlgorithms/Java / bfprt

Method bfprt

src/main/java/com/thealgorithms/others/BFPRT.java:71–84  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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.

Callers 2

getMinKthByBFPRTMethod · 0.95
medianOfMediansMethod · 0.95

Calls 2

medianOfMediansMethod · 0.95
partitionMethod · 0.95

Tested by

no test coverage detected