1
1
package com .thealgorithms .others ;
2
2
3
- import java .util .Arrays ;
4
-
5
3
/**
6
- * BFPRT algorithm.
4
+ * The BFPRT (Median of Medians) algorithm implementation.
5
+ * It provides a way to find the k-th smallest element in an unsorted array
6
+ * with an optimal worst-case time complexity of O(n).
7
+ * This algorithm is used to find the k smallest numbers in an array.
7
8
*/
8
9
public final class BFPRT {
9
10
private BFPRT () {
10
11
}
11
12
13
+ /**
14
+ * Returns the k smallest elements from the array using the BFPRT algorithm.
15
+ *
16
+ * @param arr the input array
17
+ * @param k the number of smallest elements to return
18
+ * @return an array containing the k smallest elements, or null if k is invalid
19
+ */
12
20
public static int [] getMinKNumsByBFPRT (int [] arr , int k ) {
13
21
if (k < 1 || k > arr .length ) {
14
22
return null ;
15
23
}
16
24
int minKth = getMinKthByBFPRT (arr , k );
17
25
int [] res = new int [k ];
18
26
int index = 0 ;
19
- for (int i = 0 ; i < arr . length ; i ++ ) {
20
- if (arr [ i ] < minKth ) {
21
- res [index ++] = arr [ i ] ;
27
+ for (int value : arr ) {
28
+ if (value < minKth ) {
29
+ res [index ++] = value ;
22
30
}
23
31
}
24
32
for (; index != res .length ; index ++) {
@@ -27,17 +35,39 @@ public static int[] getMinKNumsByBFPRT(int[] arr, int k) {
27
35
return res ;
28
36
}
29
37
38
+ /**
39
+ * Returns the k-th smallest element from the array using the BFPRT algorithm.
40
+ *
41
+ * @param arr the input array
42
+ * @param k the rank of the smallest element to find
43
+ * @return the k-th smallest element
44
+ */
30
45
public static int getMinKthByBFPRT (int [] arr , int k ) {
31
46
int [] copyArr = copyArray (arr );
32
47
return bfprt (copyArr , 0 , copyArr .length - 1 , k - 1 );
33
48
}
34
49
50
+ /**
51
+ * Creates a copy of the input array.
52
+ *
53
+ * @param arr the input array
54
+ * @return a copy of the array
55
+ */
35
56
public static int [] copyArray (int [] arr ) {
36
57
int [] copyArr = new int [arr .length ];
37
58
System .arraycopy (arr , 0 , copyArr , 0 , arr .length );
38
59
return copyArr ;
39
60
}
40
61
62
+ /**
63
+ * BFPRT recursive method to find the k-th smallest element.
64
+ *
65
+ * @param arr the input array
66
+ * @param begin the starting index
67
+ * @param end the ending index
68
+ * @param i the index of the desired smallest element
69
+ * @return the k-th smallest element
70
+ */
41
71
public static int bfprt (int [] arr , int begin , int end , int i ) {
42
72
if (begin == end ) {
43
73
return arr [begin ];
@@ -54,12 +84,12 @@ public static int bfprt(int[] arr, int begin, int end, int i) {
54
84
}
55
85
56
86
/**
57
- * wikipedia: https://en.wikipedia.org/wiki/Median_of_medians .
87
+ * Finds the median of medians as the pivot element .
58
88
*
59
- * @param arr an array.
60
- * @param begin begin num.
61
- * @param end end num.
62
- * @return median of medians.
89
+ * @param arr the input array
90
+ * @param begin the starting index
91
+ * @param end the ending index
92
+ * @return the median of medians
63
93
*/
64
94
public static int medianOfMedians (int [] arr , int begin , int end ) {
65
95
int num = end - begin + 1 ;
@@ -71,12 +101,15 @@ public static int medianOfMedians(int[] arr, int begin, int end) {
71
101
return bfprt (mArr , 0 , mArr .length - 1 , mArr .length / 2 );
72
102
}
73
103
74
- public static void swap (int [] arr , int i , int j ) {
75
- int swap = arr [i ];
76
- arr [i ] = arr [j ];
77
- arr [j ] = swap ;
78
- }
79
-
104
+ /**
105
+ * Partitions the array around a pivot.
106
+ *
107
+ * @param arr the input array
108
+ * @param begin the starting index
109
+ * @param end the ending index
110
+ * @param num the pivot element
111
+ * @return the range where the pivot is located
112
+ */
80
113
public static int [] partition (int [] arr , int begin , int end , int num ) {
81
114
int small = begin - 1 ;
82
115
int cur = begin ;
@@ -90,19 +123,31 @@ public static int[] partition(int[] arr, int begin, int end, int num) {
90
123
cur ++;
91
124
}
92
125
}
93
- int [] pivotRange = new int [2 ];
94
- pivotRange [0 ] = small + 1 ;
95
- pivotRange [1 ] = big - 1 ;
96
- return pivotRange ;
126
+ return new int [] {small + 1 , big - 1 };
97
127
}
98
128
129
+ /**
130
+ * Finds the median of the elements between the specified range.
131
+ *
132
+ * @param arr the input array
133
+ * @param begin the starting index
134
+ * @param end the ending index
135
+ * @return the median of the specified range
136
+ */
99
137
public static int getMedian (int [] arr , int begin , int end ) {
100
138
insertionSort (arr , begin , end );
101
139
int sum = begin + end ;
102
140
int mid = sum / 2 + (sum % 2 );
103
141
return arr [mid ];
104
142
}
105
143
144
+ /**
145
+ * Sorts a portion of the array using insertion sort.
146
+ *
147
+ * @param arr the input array
148
+ * @param begin the starting index
149
+ * @param end the ending index
150
+ */
106
151
public static void insertionSort (int [] arr , int begin , int end ) {
107
152
if (arr == null || arr .length < 2 ) {
108
153
return ;
@@ -118,29 +163,16 @@ public static void insertionSort(int[] arr, int begin, int end) {
118
163
}
119
164
}
120
165
121
- public static void main (String [] args ) {
122
- int [] arr = {
123
- 11 ,
124
- 9 ,
125
- 1 ,
126
- 3 ,
127
- 9 ,
128
- 2 ,
129
- 2 ,
130
- 5 ,
131
- 6 ,
132
- 5 ,
133
- 3 ,
134
- 5 ,
135
- 9 ,
136
- 7 ,
137
- 2 ,
138
- 5 ,
139
- 5 ,
140
- 1 ,
141
- 9 ,
142
- };
143
- int [] minK = getMinKNumsByBFPRT (arr , 5 );
144
- System .out .println (Arrays .toString (minK ));
166
+ /**
167
+ * Swaps two elements in an array.
168
+ *
169
+ * @param arr the input array
170
+ * @param i the index of the first element
171
+ * @param j the index of the second element
172
+ */
173
+ public static void swap (int [] arr , int i , int j ) {
174
+ int temp = arr [i ];
175
+ arr [i ] = arr [j ];
176
+ arr [j ] = temp ;
145
177
}
146
178
}
0 commit comments