Here you can find the source of medianOfMedians(int[] array)
public static int medianOfMedians(int[] array)
//package com.java2s; //License from project: Apache License public class Main { /**/*from w ww . j a v a2 s . c o m*/ * Finds the median of medians in the given array. * * @return the index of the median of medians. */ public static int medianOfMedians(int[] array) { final int splitSize = array.length / 5; if (splitSize <= 2) { radixSort(array); return array[array.length / 2]; } int[] pivots = new int[splitSize]; for (int i = 0; i < splitSize; i++) { final int start = i * 5; final int end = i * 5 + 5; pivots[i] = partition(array, start, end); } return pivots[splitSize / 2]; } /** * Radix sorts an integer array in O(m*n), where m is the length of the key * (here 32 bit) and n the number of elements. It only works for positive * numbers, so please don't come up with negative numbers, it will result in * array out of bound exceptions, since they don't have an array index. */ public static void radixSort(int[] a) { int[] nPart = new int[2]; int[][] part = new int[2][a.length]; for (int i = 0; i < 32; i++) { nPart[0] = 0; nPart[1] = 0; for (int anA : a) { int n = (anA >> i) & 1; part[n][nPart[n]++] = anA; } System.arraycopy(part[0], 0, a, 0, nPart[0]); System.arraycopy(part[1], 0, a, nPart[0], nPart[1]); } } /** * Partitions the given array in-place and uses the last element as pivot, * everything less than the pivot will be placed left and everything greater * will be placed right of the pivot. It returns the index of the pivot * element after partitioning. */ public static <T extends Comparable<T>> int partition(T[] array) { return partition(array, 0, array.length); } /** * Partitions the given array in-place and uses the end element as pivot, * everything less than the pivot will be placed left and everything greater * will be placed right of the pivot. It returns the index of the pivot * element after partitioning. */ public static <T extends Comparable<T>> int partition(T[] array, int start, int end) { final int ending = end - 1; final T x = array[ending]; int i = start - 1; for (int j = start; j < ending; j++) { if (array[j].compareTo(x) < 0) { i++; swap(array, i, j); } } i++; swap(array, i, ending); return i; } /** * Partitions the given array in-place and uses the last element as pivot, * everything less than the pivot will be placed left and everything greater * will be placed right of the pivot. It returns the index of the pivot * element after partitioning. */ public static int partition(int[] array) { return partition(array, 0, array.length); } /** * Partitions the given array in-place and uses the end element as pivot, * everything less than the pivot will be placed left and everything greater * will be placed right of the pivot. It returns the index of the pivot * element after partitioning. */ public static int partition(int[] array, int start, int end) { final int ending = end - 1; final int x = array[ending]; int i = start - 1; for (int j = start; j < ending; j++) { if (array[j] <= x) { i++; swap(array, i, j); } } i++; swap(array, i, ending); return i; } /** * Partitions the given array in-place and uses the last element as pivot, * everything less than the pivot will be placed left and everything greater * will be placed right of the pivot. It returns the index of the pivot * element after partitioning. */ public static int partition(long[] array) { return partition(array, 0, array.length); } /** * Partitions the given array in-place and uses the end element as pivot, * everything less than the pivot will be placed left and everything greater * will be placed right of the pivot. It returns the index of the pivot * element after partitioning. */ public static int partition(long[] array, int start, int end) { final int ending = end - 1; final long x = array[ending]; int i = start - 1; for (int j = start; j < ending; j++) { if (array[j] <= x) { i++; swap(array, i, j); } } i++; swap(array, i, ending); return i; } /** * Partitions the given array in-place and uses the last element as pivot, * everything less than the pivot will be placed left and everything greater * will be placed right of the pivot. It returns the index of the pivot * element after partitioning. */ public static int partition(double[] array) { return partition(array, 0, array.length); } /** * Partitions the given array in-place and uses the end element as pivot, * everything less than the pivot will be placed left and everything greater * will be placed right of the pivot. It returns the index of the pivot * element after partitioning. */ public static int partition(double[] array, int start, int end) { final int ending = end - 1; final double x = array[ending]; int i = start - 1; for (int j = start; j < ending; j++) { if (array[j] <= x) { i++; swap(array, i, j); } } i++; swap(array, i, ending); return i; } /** * Swaps the given indices x with y in the array. */ public static void swap(int[] array, int x, int y) { int tmpIndex = array[x]; array[x] = array[y]; array[y] = tmpIndex; } /** * Swaps the given indices x with y in the array. */ public static void swap(long[] array, int x, int y) { long tmpIndex = array[x]; array[x] = array[y]; array[y] = tmpIndex; } /** * Swaps the given indices x with y in the array. */ public static void swap(double[] array, int x, int y) { double tmpIndex = array[x]; array[x] = array[y]; array[y] = tmpIndex; } /** * Swaps the given indices x with y in the array. */ public static void swap(boolean[] array, int x, int y) { boolean tmpIndex = array[x]; array[x] = array[y]; array[y] = tmpIndex; } /** * Swaps the given indices x with y in the array. */ public static <T> void swap(T[] array, int x, int y) { T tmpIndex = array[x]; array[x] = array[y]; array[y] = tmpIndex; } }