假设有一个数组,现求出数组中最大的前n个数的下标。输入:一个数组 Input[]
输出:一个数组 Output[]注意:
n是一个未知数。
Input中的数,很具有区分度,大小差别挺大。
Input中的数,有重复的。例:
int Input[] ={1, 1, 2, 2, 3, 4, 5, 6, 7, 8, 9, 200, 12, 180, 190, 220, 221, 3, 5, 226};
// 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,18,19//这里是对应的坐标 public static int[] Compute(int Input[]){
}输出
Ouput={11,13,14,15,16,19}Output是较大的几个数对应的下标.
可能这是一个算法题。
输出:一个数组 Output[]注意:
n是一个未知数。
Input中的数,很具有区分度,大小差别挺大。
Input中的数,有重复的。例:
int Input[] ={1, 1, 2, 2, 3, 4, 5, 6, 7, 8, 9, 200, 12, 180, 190, 220, 221, 3, 5, 226};
// 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,18,19//这里是对应的坐标 public static int[] Compute(int Input[]){
}输出
Ouput={11,13,14,15,16,19}Output是较大的几个数对应的下标.
可能这是一个算法题。
解决方案 »
- java中的set和get方法和构造函数什么时候一块用?
- 关于一个正则表达式,我的错在哪里?
- 高分求救:RCP程序中调用单态类问题。
- 请问各位:用java的FileWriter类对文件进行写操作,如果写的时候硬盘空间满,如何能得到硬盘已满的出错信息?
- java怎样实现vc中GroupBox控件的功能?
- 我是个初学者,求一本学习JAVA的好书!(中文的)
- 请问我的程序为什么看不到结果??、
- SQL Server中的datetime类型如何通过JDBC获得??
- 这段程序怎么不能运行?thinking in java
- 如何恢复JTextPane的缺省样式?
- 调用static方法的for循环——改错
- jsp/html如何在页面显示能播放MP4的播放器 大家进来解决一下 急呀
public class SelectSort {
// 简单选择排序
static void selectSort(int[] array, int[] subscript, int n) {
int i, j, max, tmp;
for (i = 0; i < n; i++) {
max = i;
for (j = i; j < array.length - 1; j++) {
if (array[max] < array[j + 1])
max = j + 1;
}
if (max != i) {
tmp = array[i];
array[i] = array[max];
array[max] = tmp;
tmp = subscript[i];
subscript[i] = subscript[max];
subscript[max] = tmp;
}
}
} public static void main(String[] args) {
int[] input = { 1, 1, 2, 2, 3, 4, 5, 6, 7, 8, 9, 200, 12, 180, 190,
220, 221, 3, 5, 226 };
int[] subscript = new int[input.length];
for(int i = 0; i < subscript.length; ++i)
subscript[i] = i;
int n = 6;
selectSort(input, subscript, n);
for(int i = 0; i < n; ++i)
System.out.print(subscript[i] + " ");
System.out.println();
}}
数组规模M,取前n大的数的时间复杂度为O(M*lg(n))