Here you can find the source of sumMaxK(double[] x, int k)
public static double sumMaxK(double[] x, int k)
//package com.java2s; //License from project: Apache License public class Main { public static double sumMaxK(double[] x, int k) { int[] ids = sort(x); if (k > ids.length) { k = ids.length;/* w w w. j av a2s . c om*/ } double s = 0; for (int i = ids.length - 1; (ids.length - 1 - i + 1) <= k; i--) { s = s + x[ids[i]]; } return s; } public static int[] sort(double[] x) { int[] id = new int[x.length]; boolean[] fid = new boolean[id.length]; int cnt = 0; for (int i = 0; i < id.length; i++) { int mi = 0; double min = x[mi]; for (int j = 0; j < x.length; j++) { if (fid[j]) { continue; } if (fid[mi]) { mi = j; min = x[j]; } else { if (min > x[j]) { mi = j; min = x[j]; } } } fid[mi] = true; id[cnt++] = mi; } return id; } }