用Java写一个冒泡算法并且写好注释
时间: 2024-05-20 10:17:02 浏览: 17
/**
* 冒泡排序算法
* @param arr 待排序的数组
*/
public static void bubbleSort(int[] arr) {
// 外层循环控制排序的轮数
for (int i = 0; i < arr.length - 1; i++) {
// 内层循环控制每一轮排序的次数
for (int j = 0; j < arr.length - 1 - i; j++) {
// 如果前一个元素比后一个元素大,就交换它们的位置
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
相关问题
用Java写一个冒泡算法并且写好每行的注释
以下是Java语言实现冒泡排序算法的代码及注释:
```
public static void bubbleSort(int[] array) {
int n = array.length;
for (int i = 0; i < n - 1; i++) {
// 执行n-1轮比较,每轮确定一个最大值
for (int j = 0; j < n - 1 - i; j++) {
// 比较相邻两个元素的大小,如果前一个元素比后一个元素大,则交换它们的位置
if (array[j] > array[j + 1]) {
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
}
```
注释说明:
1. `public static`:定义了一个公有的静态方法,可以在其他类中直接调用。
2. `void`:表示该方法没有返回值。
3. `bubbleSort`:方法名,用于表示执行的操作。
4. `(int[] array)`:表示该方法接受一个整型数组作为参数。
5. `int n = array.length;`:获取数组的长度,用于确定循环次数。
6. `for (int i = 0; i < n - 1; i++)`:外层循环,循环次数为数组长度减一,每轮确定一个最大值。
7. `for (int j = 0; j < n - 1 - i; j++)`:内层循环,循环次数为数组长度减去已排好序的元素个数减一,每轮比较相邻两个元素的大小。
8. `if (array[j] > array[j + 1])`:如果前一个元素比后一个元素大,则交换它们的位置。
9. `int temp = array[j]; array[j] = array[j + 1]; array[j + 1] = temp;`:交换元素位置,使用中间变量temp来存储需要交换的元素。
10. `}`:循环结束标志。
11. `}`:方法结束标志。
用java写一个冒泡排序做好注释
以下是用 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 = {5, 2, 8, 4, 7, 1, 3, 9, 6};
bubbleSort(arr);
// 输出排序后的数组
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}
```
注释解释:
- `bubbleSort()` 方法是冒泡排序算法的具体实现,接收一个整型数组作为参数。
- `n` 表示数组的长度。
- 外层循环 `for (int i = 0; i < n - 1; i++)` 控制比较轮数,因为每一轮都会将最大值或最小值移到数组的末尾,所以比较轮数为 `n - 1`。
- 内层循环 `for (int j = 0; j < n - i - 1; j++)` 控制每轮比较次数。因为每轮比较都会将最大值或最小值移到数组的末尾,所以每一轮都比上一轮少一个元素需要比较,故比较次数为 `n - i - 1`。
- `if (arr[j] > arr[j+1])` 判断前一个数是否比后一个数大,如果是,则交换两个数的位置。
- `main()` 方法中的测试用例 `int[] arr = {5, 2, 8, 4, 7, 1, 3, 9, 6};` 是一个随机数组。
- `for` 循环输出排序后的数组。
相关推荐
![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)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)