给定一个100的整数的数组,需要找出最大10个数,使用冒泡排序或选择排序编码实现 用Java
时间: 2024-05-05 17:17:52 浏览: 87
好的,这是一个关于排序的问题,非常适合使用冒泡排序或选择排序来解决。下面是使用Java语言实现的代码:
```java
import java.util.Arrays;
public class Top10Numbers {
public static void main(String[] args) {
int[] array = {1, 5, 3, 8, 2, 9, 7, 4, 6, 10, 15, 12, 11, 13, 14, 18, 16, 19, 20, 17, 25, 23, 21, 22, 24, 30, 28, 26, 27, 29, 35, 32, 31, 33, 34, 40, 38, 36, 37, 39, 45, 43, 41, 42, 44, 50, 48, 46, 47, 49, 55, 53, 51, 52, 54, 60, 58, 56, 57, 59, 65, 63, 61, 62, 64, 70, 68, 66, 67, 69, 75, 73, 71, 72, 74, 80, 78, 76, 77, 79, 85, 83, 81, 82, 84, 90, 88, 86, 87, 89, 95, 93, 91, 92, 94, 100, 98, 96, 97, 99};
int k = 10;
int[] topK = new int[k];
// 使用冒泡排序
for (int i = 0; i < k; i++) {
for (int j = i + 1; j < array.length; j++) {
if (array[i] < array[j]) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
topK[i] = array[i];
}
System.out.println("最大的10个数是:" + Arrays.toString(topK));
}
}
```
运行结果:
```
最大的10个数是:[100, 99, 98, 97, 96, 95, 94, 93, 92, 91]
```
希望能够帮助到您!
阅读全文