Java将int a[]={96,3,56,39,15,85}从小到大排序并输出
时间: 2023-11-07 07:04:43 浏览: 34
好的,以下是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
```
相关问题
将int a[]={96,3,56,39,15,85}从小到大排序并输出
好的,您可以使用冒泡排序算法,按照从小到大的顺序将数组元素进行排序,具体代码如下:
```
#include<stdio.h>
void bubble_sort(int arr[], int len)
{
int i, j, temp;
for (i = 0; i < len - 1; i++)
{
for (j = 0; j < len - i - 1; 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(int);
int i;
bubble_sort(a, len);
printf("从小到大排序后的结果为:");
for (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}从小到大排序
您好,可以使用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
```
相关推荐
![](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)