用java写一个冒泡排序
时间: 2023-09-20 16:05:48 浏览: 71
### 回答1:
好的,以下是Java代码实现冒泡排序:
```java
public class BubbleSort {
public static void bubbleSort(int[] nums) {
if (nums == null || nums.length == 0) {
return;
}
int len = nums.length;
for (int i = 0; i < len - 1; i++) {
for (int j = 0; j < len - 1 - i; j++) {
if (nums[j] > nums[j + 1]) {
int temp = nums[j];
nums[j] = nums[j + 1];
nums[j + 1] = temp;
}
}
}
}
public static void main(String[] args) {
int[] nums = {10, 8, 6, 4, 2};
bubbleSort(nums);
for (int num : nums) {
System.out.print(num + " ");
}
}
}
```
以上代码实现了传入一个整型数组 `nums`,然后对其进行冒泡排序的功能。 `bubbleSort`方法中的两个for循环实现了冒泡排序的核心逻辑。外层循环控制比较的轮数,内层循环则是每一轮比较中具体比较的过程。
程序的输出为:`2 4 6 8 10`,表示对于传入的整型数组进行冒泡排序后的结果。
### 回答2:
冒泡排序是一种简单的排序算法,它重复地比较相邻的两个元素,如果顺序错误就交换位置,直到没有交换为止。以下是用Java编写冒泡排序的示例代码:
```java
public class BubbleSort {
public static void bubbleSort(int[] array) {
int n = array.length;
for (int i = 0; i < n-1; i++) {
for (int j = 0; j < n-i-1; j++) {
if (array[j] > array[j+1]) {
// 交换array[j]和array[j+1]的位置
int temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
}
public static void main(String[] args) {
int[] array = {64, 34, 25, 12, 22, 11, 90};
bubbleSort(array);
System.out.println("排序后的数组:");
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
}
}
```
以上是一个简单的冒泡排序的Java实现。在`bubbleSort`方法中,我们使用两个嵌套的for循环来遍历数组并比较相邻元素的大小。如果相邻元素的顺序错误,我们就交换它们的位置。最后,通过多次遍历和交换,最大的元素会逐渐“冒泡”到数组的末尾。在`main`方法中,我们调用`bubbleSort`方法对给定的数组进行排序,并输出排序后的结果。
### 回答3:
冒泡排序是一种基础的排序算法,它通过比较相邻的元素并交换位置,将最大(或最小)的元素逐渐“冒泡”到数列的一端。以下是使用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]) {
// 交换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 = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}
```
运行以上代码,可以得到以下结果:
```
排序结果:
11 12 22 25 34 64 90
```
该冒泡排序算法的时间复杂度为O(n^2),其中n为待排序数组的长度。虽然冒泡排序算法的效率不高,但由于其实现简单直观,易于理解,因此仍然被广泛使用。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![ppt](https://img-home.csdnimg.cn/images/20210720083527.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)