java给int a[]={96,3, 56,39,15,85}从小到大排序
时间: 2023-12-14 20:04:02 浏览: 22
您好,可以使用Arrays.sort()方法实现从小到大排序,具体实现代码如下:
```
import java.util.Arrays;
public class SortExample {
public static void main(String[] args) {
int[] a = {96, 3, 56, 39, 15, 85};
Arrays.sort(a);
System.out.print("从小到大排序结果为:");
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
System.out.println();
}
}
```
输出结果为:
```
从小到大排序结果为:3 15 39 56 85 96
```
相关问题
给int a[]={96,3, 56,39,15,85}从小到大排序
您好,可以使用冒泡排序算法实现从小到大的排序,具体实现代码如下:
```
#include <stdio.h>
void bubbleSort(int arr[], int len){
int i, j, temp;
for (i = 0; i < len - 1; i++) {
for (j = 0; j < len - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
// 交换相邻两个数
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main(){
int a[] = {96, 3, 56, 39, 15, 85};
int len = sizeof(a) / sizeof(a[0]);
bubbleSort(a, len);
printf("从小到大排序结果为:");
for (int i = 0; i < len; i++) {
printf("%d ", a[i]);
}
printf("\n");
return 0;
}
```
输出结果为:
```
从小到大排序结果为:3 15 39 56 85 96
```
Java将int a[]={96,3,56,39,15,85}从小到大排序并输出
好的,以下是Java中使用冒泡排序算法对int数组从小到大进行排序并输出的代码:
```
public class Main {
public static void main(String[] args) {
int[] a = {96, 3, 56, 39, 15, 85};
int temp;
for (int i = 0; i < a.length - 1; i++) {
for (int j = 0; j < a.length - i - 1; j++) {
if (a[j] > a[j + 1]) {
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
System.out.print("从小到大排序后的结果为:");
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
}
}
```
运行上述代码,您将会得到输出结果:
```
从小到大排序后的结果为:3 15 39 56 85 96
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)