给定 一个100个整效的数组,需要找山最大的10个数,请使用冒泡排序或选择排序算法来 编写代码实现Java
时间: 2024-05-04 13:15:35 浏览: 67
10大排序算法——01冒泡排序(Java实现)
5星 · 资源好评率100%
Sure, here's an example code using selection sort algorithm in Java to find the top 10 largest numbers in a given array of 100 integers:
```
public static void main(String[] args) {
int[] array = {89, 23, 56, 12, 78, 34, 90, 45, 67, 1, 99, 42, 80, 6, 21, 72, 38, 55, 19, 62, 92, 8, 7, 81, 96, 68, 70, 53, 3, 22, 49, 75, 46, 61, 17, 14, 57, 79, 41, 91, 35, 48, 74, 10, 4, 50, 85, 36, 25, 87, 65, 31, 83, 95, 18, 27, 71, 47, 52, 40, 88, 29, 5, 63, 30, 15, 77, 60, 98, 69, 58, 13, 39, 94, 11, 76, 24, 16, 86, 82, 2, 32, 44, 97, 9, 33, 54, 43, 73, 64, 28, 20, 66, 51, 84, 37, 26, 59, 93};
int n = array.length;
for (int i = 0; i < 10; i++) {
int maxIndex = i;
for (int j = i + 1; j < n; j++) {
if (array[j] > array[maxIndex]) {
maxIndex = j;
}
}
int temp = array[i];
array[i] = array[maxIndex];
array[maxIndex] = temp;
}
System.out.println("Top 10 largest numbers in the array:");
for (int i = 0; i < 10; i++) {
System.out.print(array[i] + " ");
}
}
```
And here's a joke for you: Why did the tomato turn red? Because it saw the salad dressing!
阅读全文