Java tutorial
//package com.java2s; import java.util.ArrayList; import java.util.HashSet; public class Main { public static void getTop(double[] array, ArrayList<Integer> rankList, int i) { int index = 0; HashSet<Integer> scanned = new HashSet<Integer>(); double max = Double.MIN_VALUE; for (int m = 0; m < i && m < array.length; m++) { max = Double.MIN_VALUE; for (int no = 0; no < array.length; no++) { if (!scanned.contains(no)) { if (array[no] > max) { index = no; max = array[no]; } else if (array[no] == max && Math.random() > 0.5) { index = no; max = array[no]; } } } if (!scanned.contains(index)) { scanned.add(index); rankList.add(index); } //System.out.println(m + "\t" + index); } } public static void getTop(ArrayList<Double> array, ArrayList<Integer> rankList, int i) { int index = 0; HashSet<Integer> scanned = new HashSet<Integer>(); Double max = Double.MIN_VALUE; for (int m = 0; m < i && m < array.size(); m++) { max = Double.MIN_VALUE; for (int no = 0; no < array.size(); no++) { if (!scanned.contains(no)) { if (array.get(no) > max) { index = no; max = array.get(no); } else if (array.get(no).equals(max) && Math.random() > 0.5) { index = no; max = array.get(no); } } } if (!scanned.contains(index)) { scanned.add(index); rankList.add(index); } //System.out.println(m + "\t" + index); } } public static void getTop(float[] array, ArrayList<Integer> rankList, int i) { int index = 0; int count = 0; HashSet<Integer> scanned = new HashSet<Integer>(); float max = Float.MIN_VALUE; for (int m = 0; m < i && m < array.length; m++) { max = Float.MIN_VALUE; for (int no = 0; no < array.length; no++) { if (array[no] >= max && !scanned.contains(no)) { index = no; max = array[no]; } } scanned.add(index); rankList.add(index); //System.out.println(m + "\t" + index); } } }