java输出一个冒泡排序
时间: 2023-12-23 08:46:00 浏览: 26
的代码。
以下是Java的冒泡排序代码:
```java
public static void bubbleSort(int[] arr) {
int n = arr.length;
for(int i = 0; i < n-1; i++) {
for(int j = 0; j < n-i-1; j++) {
if(arr[j] > arr[j+1]) {
// swap arr[j] and arr[j+1]
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
```
希望我的回答能够帮到你!接下来想听一个笑话吗?
相关问题
Java写一个冒泡排序
### 回答1:
冒泡排序是一种简单的排序算法,可以用 Java 语言来实现。以下是一个基于 Java 的冒泡排序实现:
```java
public class BubbleSort {
public static void main(String[] args) {
int[] arr = {5, 2, 7, 3, 9, 4, 1, 8, 6};
bubbleSort(arr);
System.out.println(Arrays.toString(arr));
}
public static void bubbleSort(int[] arr) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// 交换 arr[j] 和 arr[j+1]
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
}
```
在上面的示例中,我们首先定义一个 `arr` 数组,然后调用 `bubbleSort` 方法来对数组进行排序。在 `bubbleSort` 方法中,我们使用两个嵌套循环来实现冒泡排序。外层循环控制排序的轮数,内层循环控制每轮比较的次数。如果相邻两个数的顺序不正确,则交换它们的位置。
最后,我们将排序后的数组输出到控制台。
### 回答2:
冒泡排序是一种经典的排序算法,它通过不断比较相邻元素的大小,并且交换位置,使得最大(或最小)的元素逐步“冒泡”到数组的末尾。以下是用Java语言实现冒泡排序的代码:
```java
public class BubbleSort {
public static void bubbleSort(int[] arr) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// 交换相邻元素的位置
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
public static void main(String[] args) {
int[] arr = {64, 34, 25, 12, 22, 11, 90};
bubbleSort(arr);
System.out.println("排序后的数组:");
for (int i : arr) {
System.out.print(i + " ");
}
}
}
```
以上的代码中,我们定义了一个`bubbleSort`方法,它接收一个整数数组作为参数,并对该数组进行冒泡排序操作。在主函数中,我们定义了一个整数数组`arr`,并调用`bubbleSort`方法对其进行排序。最后,我们使用循环打印出排序后的数组。
运行以上代码,输出为:
```
排序后的数组:
11 12 22 25 34 64 90
```
这就是使用Java实现冒泡排序的方法。冒泡排序比较简单,但在实际应用中效率较低,因此通常不会在大型数据集上使用。
java写一个冒泡排序
好的,这里是 Java 写一个冒泡排序的示例代码:
```java
public class BubbleSort {
public static void main(String[] args) {
int[] arr = {64, 34, 25, 12, 22, 11, 90};
bubbleSort(arr);
System.out.println("排序后的数组:");
for(int i=0; i<arr.length; i++){
System.out.print(arr[i] + " ");
}
}
public static void bubbleSort(int[] arr){
int temp = 0;
for(int i=0; i<arr.length-1; i++){
for(int j=0; j<arr.length-i-1; j++){
if(arr[j] > arr[j+1]){
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
}
```
以上代码中,`bubbleSort` 方法使用冒泡排序算法对传入的数组进行排序,排序结果在 `main` 方法中进行输出。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)
![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_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)