-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBubble_sort.java
More file actions
68 lines (57 loc) · 1.48 KB
/
Bubble_sort.java
File metadata and controls
68 lines (57 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package com.cpucode.java.array;/*
* @Author: cpu_code
* @Date: 2020-07-08 10:08:39
* @LastEditTime: 2020-07-08 10:24:19
* @FilePath: \java\basics\bubble_sort.java
* @Gitee: https://gitee.com/cpu_code
* @CSDN: https://blog.csdn.net/qq_44226094
*/
/**
* 冒泡排序
*/
public class Bubble_sort
{
public static void main(String[] args)
{
int[] arr = { 9, 8, 3, 5, 2 };
System.out.print("冒泡排序前 :");
printArray(arr); // 打印数组元素
bubbleSort(arr); // 调用排序方法
System.out.print("冒泡排序后 :");
printArray(arr); // 打印数组元素
}
// 定义打印数组元素的方法
public static void printArray(int[] arr)
{
// 循环遍历数组的元素
for (int i = 0; i < arr.length; i++)
{
System.out.print(arr[i] + " "); // 打印元素和空格
}
System.out.print("\n");
}
// 定义对数组排序的方法
public static void bubbleSort(int[] arr)
{
int i = 0;
int j = 0;
// 定义外层循环
for (i = 0; i < arr.length - 1; i++)
{
// 定义内层循环
for (j = 0; j < arr.length - i - 1; j++)
{
// 比较相邻元素
if (arr[j] > arr[j + 1])
{
// 下面的三行代码用于交换两个元素
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
System.out.print("第" + (i + 1) + "轮排序后:");
printArray(arr); // 每轮比较结束打印数组元素
}
}
}