Java 在小于O(n^2)的范围内对给定整数列表进行排序
如何将列组分配给给定整数列表中的元素? 是否有时间复杂度小于^2的算法Java 在小于O(n^2)的范围内对给定整数列表进行排序,java,arrays,algorithm,time-complexity,Java,Arrays,Algorithm,Time Complexity,如何将列组分配给给定整数列表中的元素? 是否有时间复杂度小于^2的算法 Input: 2 6 7 3 9 Output: 1 3 4 2 5 通过排序,您可以在*log n中轻松完成此操作: int[] input = //... int[] input = new int[]{2, 6, 7, 3, 9}; Integer[] indices = new Integer[input.length]; for (int i = 0; i < indices.length; i++) {
Input: 2 6 7 3 9
Output: 1 3 4 2 5
通过排序,您可以在*log n中轻松完成此操作:
int[] input = //...
int[] input = new int[]{2, 6, 7, 3, 9};
Integer[] indices = new Integer[input.length];
for (int i = 0; i < indices.length; i++) {
indices[i] = i;
}
// find permutation of indices that would yield sorted input
Arrays.sort(indices, Comparator.comparingInt(i -> input[i]));
// invert permutation and add 1 to get rank
int[] ranks = new int[indices.length];
for (int i = 0; i < indices.length; i++) {
ranks[indices[i]] = i+1;
}
你给他们排名是什么意思?规范化