удалил комментарии, которые мне не помогли. Может быть, вы можете добавить лучшие комментарии.
import java.util.Arrays;
public class Pivot
{
static int thepivotpoint;
public static void main(String[] args)
{
int[] list = {17, 10, 44, 7, 7, 33, 24, 10, 48, 49 };
thepivotpoint = 0;
System.out.println (select4 (list, list.length, 3));
}
public static int select4 (int[] list, int high, int k)
{
return selection2 (list, 0, high, k);
// return selection2 (list, 1, high, k);
}
public static int selection2 (int[] list, int low, int high, int k)
{
if (high == low)
return list[low];
partition2 (list, low, high);
if (k == thepivotpoint)
return list [thepivotpoint];
if (k < thepivotpoint)
return selection2 (list, low, thepivotpoint - 1, k);
return selection2 (list, thepivotpoint + 1, high, k);
}
static int count = 0;
public static void show (int [] T)
{
for (int i : T)
System.out.print (i + "\t");
System.out.println ();
if (++count > 20) System.exit (1);
}
public static void partition2 (int[] list, int low, int high)
{
int arraysize = high - low;
int r = (int) Math.ceil (arraysize / 5);
int [] T = new int[r+1];
for (int i = 1; i <= r; i++)
{
int first = low + 5 * i - 5;
int last = Math.min (low + 5 * i - 1, arraysize);
T [i] = median (list, first, last);
}
show (list);
approximateTheMedian (T, r, low, high, list);
}
public static void approximateTheMedian (int [] T, int r, int low, int high, int [] list)
{
int pivotitem = select4 (T, r, (r + 1) / 2);
int j = low;
int mark = 0;
for (int i = low; i < high; i++)
{
if (list[i] == pivotitem)
{
list = swap (i, j, list);
mark = j; //mark where pivotitem placed
j++;
}
else if (list[i] < pivotitem)
{
list = swap (i, j, list);
j++;
}
}
thepivotpoint = j - 1;
list = swap (mark, thepivotpoint, list);
}
public static int median (int[] list, int start, int end)
{
int [] copy = (int[]) list.clone ();
Arrays.sort (copy);
return copy [(start + end) / 2];
}
public static int[] swap (int one, int two, int[] list)
{
int dummy = list[one];
list[one] = list[two];
list[two] = dummy;
return list;
}
}