Java tutorial
//package com.java2s; import java.util.Arrays; public class Main { public static void top_k_competition(float[] response_array, int size, int k) { float[] copy_of_array = new float[size]; System.arraycopy(response_array, 0, copy_of_array, 0, size); Arrays.sort(copy_of_array); for (int i = 0; i < size; i++) { if (response_array[i] < copy_of_array[size - k]) { response_array[i] = 0; } else { //response_array[i]= (response_array[i]- copy_of_array[size-k-1])/ // (copy_of_array[size-1]-copy_of_array[size-k-1]); response_array[i] = 1; } } } public static void top_k_competition(float[][][] response_array, int k) { int depth = response_array.length; int height = response_array[0].length; int width = response_array[0][0].length; int find_min_flag = 0; float[] copy_of_array = new float[depth * height * width]; for (int depth_index = 0; depth_index < depth; depth_index++) { for (int height_index = 0; height_index < height; height_index++) { System.arraycopy(response_array[depth_index][height_index], 0, copy_of_array, (depth_index * height + height_index) * width, width); } } Arrays.sort(copy_of_array); for (int depth_index = 0; depth_index < depth; depth_index++) { for (int height_index = 0; height_index < height; height_index++) { for (int width_index = 0; width_index < width; width_index++) { if (response_array[depth_index][height_index][width_index] < copy_of_array[depth * height * width - k]) { response_array[depth_index][height_index][width_index] = 0; } else if ((response_array[depth_index][height_index][width_index] == copy_of_array[depth * height * width - k]) && find_min_flag == 1) { response_array[depth_index][height_index][width_index] = 0; } else if ((response_array[depth_index][height_index][width_index] == copy_of_array[depth * height * width - k]) && find_min_flag == 0) { response_array[depth_index][height_index][width_index] = 1; find_min_flag = 1; } else { response_array[depth_index][height_index][width_index] = 1; } } } } } }